Http://www.aspstat.com/109
Applicability: silverlight1.1 language: C #
Imagine accessing HTML elements like JavaScript? We all know that HTML elements can be accessed through javascript. This technology is the Document Object Model that we all know now-developed by W3C standards.
In fact, in Silverlight, you can also use C # code to easily access HTML elements.
First, you need to reference the following namespace:
C # code
- Using system. Windows. browser;
First, let's take a look at a static (static) object htmlpage, which provides attributes about the browser information. You can easily jump to other web pages by using the methods provided by the htmpage object as a program:
Htmlpage object
Browserinformation attributes: Browserversion browser information; platform operating system. useragent, such as MSIE or Firefox, has a version number;
Navigate Method: Jump to another page or website, such as htmlpage. navigate (http://www.aspstat.com );
You can also access the core of Dom through htmlpage.Document Object (htmldocument type), and then you can access HTML elements of the page through htmldocument.
For example, htmldocument Doc = htmlpage. Document.
Modify page properties:
For example, modify the page title:
Doc. setproperty ("title", "Bad blog ");
Or modify the background color:
Doc. setproperty ("bgcolor", "Red ");
Access HTML elements
Now, let's use the getattribute/setattribute, getstyleattribute/setstyleattribute method to access/modify the specified HTML element.
HTML element: <span id = "MSG"> Hello </span>
Silverlight in C #:
C # code
- Htmlelement ELEM = Doc. getelementbyid ("MSG ");
- // Modify content
- ELEM. setattribute ("innerhtml", "new text ");
- // Modify the background color
- ELEM. setstyleattribute ("background", "green ");
- // Modify the text color
- ELEM. setstyleattribute ("color", "White ");
HTML element: <input id = "tbname" type = "text"/>
Silverlight in C #:
C # code
- Htmlelement ELEM = Doc. getelementbyid ("tbname ");
- // Read value
- String S = ELEM. getattribute ("value ");
- // Modify the value
- ELEM. setattribute ("value", "Your name ");
HTML element:
Silverlight in C #;
C # code
- Htmlelement ELEM = Doc. getelementbyid ("imgme ");
- // Modify the image
- ELEM. setattribute ("src", "you.jpg ");
HTML element:
<Select id = "Cities"> <option> Beijing </option> <option> Shanghai </opiton> <option> Shenzhen </option> </SELECT>
Silverlight in C #:
C # code
- Htmlelement ELEM = Doc. getelementbyid ("Cities ");
- // Obtain the index of the selected Select Content
- String S = ELEM. getattribute ("selectedindex ");
Event binding
Register the event to the managed code (C #) for processing, to replace the previous JavaScript for processing. (PS: It seems that the flash can also be used, and the compiler still prefers the C # syntax and vs development IDE)
For example, use C # to handle a button event:
C # code
- Htmlelement ELEM = Doc. getelementbyid ("btbgo ");
- ELEM. attachevent ("onclick", new eventhandler
- .....
- // E. sourceelement reference time trigger object
- Void btngo_onclick (Object sender, htmleventargs E)
- {
- .....
- }
OK, complete. Silverlight makes the client's rich coal and client programming so easy. Ga, come on.