Silverlight gives us the flexibility to manage the HTML DOM from managed code. This means you can alter HTML elements using a language such as C# or Visual Basic. These languages offer features such as compile-time type-checking and richer IntelliSense support through Microsoft Visual Studio. Either way, when you consider coupling this with the ability to call managed code from JavaScript, you can see a powerful combination. This powerful combination is glued together by three classes: HtmlPage, HtmlDocument, and HtmlElement.
The HtmlPage class is the core element to use when working with the HTML DOM. This statically visible class exposes a variety of properties that give you access to the key items of a web page. These properties are listed and described in the following table.
Property |
Description |
BrowserInformation |
Gives us access to the user's browser information. |
Document |
This is an HtmlDocument object that represents a web page. This specific property represents the web page hosting the calling Silverlight application. |
Plugin |
This is an HtmlElement that represents the HTML element that is hosting the running Silverlight application. |
Window |
Gives us access to the user's browser window. This will be demonstrated shortly. |
The properties listed in the previous table serve as entry points into the HTML world. The main entry point we will use in this section is provided through the Document property. This property is an HtmlDocument object which in turn gives us two properties that serve as entry points into a web page. These properties are listed and described in the following table.
Property |
Description |
DocumentElement |
This property represents the root element of the HTML DOM. Because of this, it always represents the "HTML" element of a web page. |
Body |
This property gives us immediate access to the contents of the "BODY" element of a web page. |
Both of the elements listed in the previous table are actually HtmlElement objects. An HtmlElement is an in-memory representation of an HTML element. This means that if you have an input tag or span element in HTML, they will both be represented as HtmlElement objects in managed code. This gives us the ability to work with common, useful properties from managed code. These properties are listed in the following table.
Property |
Description |
Children |
A collection of HtmlElement items that are hosted by the current HtmlElement. |
CssClass |
The name of the CSS (Cascading Style Sheet) class in use by the HtmlElement. |
Id |
The unique identifier of the HtmlElement. |
Parent |
The HtmlElement that hosts the calling item. If the calling item is the DocumentElement, this value will be null. |
TagName |
The name of the tag used by the HtmlElement. |
The properties in the previous table define an HtmlElement. An HtmlElement is a critical component of an HtmlDocument object. This object is accessible through the statically visible HtmlPage class. With this class in hand we can perform a number of valuable tasks. Not the least of which include: finding HTML elements, setting element properties, setting CSS information, and managing state information.
Finding HTML Elements
Searching for HTML elements from managed code is an important part of taking advantage of the HTML DOM API. We can traverse through the hierarchy of a web page using the DocumentElement, Children, and Parent properties mentioned earlier. However, the HtmlDocument class exposes two methods that empower us to retrieve HtmlElement items more efficiently. These methods are listed and described in the following table.
Property |
Description |
GetElementById |
Empowers us to find any element within an HtmlDocument by referencing its unique identifier. If the element is found, an object-oriented version of the element, known as an HtmlElement, is returned. If the element is not found, null will be returned. |
GetElementsByTagName |
Finds all of the elements with a specified tag name. The results are returned as a collection of HtmlElement items. |
The previous table shows the two methods that can be used to find items within a web page. To demonstrate how to find an individual item, please look at the following code sample:
HtmlDocument document = HtmlPage.Document;
HtmlElement myHtmlElement =
document.GetElementById("myHtmlElement");
This element was assumed to be named "myHtmlElement". This name needs to be the id of an element within the hosting web page. Once we have retrieved the element though, we can actually set any of the properties that define it.
Setting Element Properties
The HtmlElement class exposes an all-purpose method called SetProperty. This method empowers us to set an element's attribute from managed code. To accomplish this, we must use two string parameters:
- The first parameter is a string that represents the name of the property to set.
- The second parameter is a string that represents the value to set to the property.
This is demonstrated in the following code sample:
HtmlDocument document = HtmlPage.Document;
HtmlElement myHtmlElement =
document.GetElementById("myHtmlElement");
myHtmlElement.SetProperty("innerText", "Hello, HTML");
This code sample sets the innerText property of an imaginary HTML element to "Hello, HTML". Essentially, imagine setting the text of a span named "myHtmlElement" to "Hello, World". This is what the code sample accomplishes. Significantly, this approach is applicable to any element in a web page.
Setting CSS Information
In addition to setting properties, we can also set an HTML element's style information from managed code. This can be accomplished by using the SetStyleAttribute method. This method takes two string parameters:
- The first parameter specifies the CSS property we want to set.
- The second parameter signals the value we want to set the property to.
This approach is demonstrated on a fictional span in the following sample:
HtmlDocument document = HtmlPage.Document;
HtmlElement myHtmlElement =
document.GetElementById("myHtmlElement");
myHtmlElement.SetStyleAttribute("color", "green");
When setting style attributes from managed code, you must use the scripting naming approach instead of the CSS naming approach to reference a style attribute. For instance, if you wanted to set the background color of an HTML element, you would need to use "background- Color" instead of "background-color".
This basic code sample changes the text color of an assumed span to green. This demonstrates how to set the CSS information of an HtmlElement. This object also allows us to set the properties that separate it from other elements in a web page. This web page is represented as an HtmlDocument. This web page may have information that is specific to the current request. For instance, the web page may display information specific to an individual customer or order. These types of situations are often times exposed through state variables.
Managing State Variables
State variables allow you to store information across page post backs. While Silverlight is a client-side platform that helps to alleviate post backs, you may still run into them. The reason why is simply because a Silverlight application is hosted within a web page. Either way, you can read state-related information through the two properties shown in the following table.
Property |
Description |
Cookies |
A string that gives you the ability to manage the cookies associated with a page. |
QueryString |
A dictionary of the key/value pairs associated with a page's query string. |
The two properties shown in the previous table are part of the HtmlDocument class. These two properties can be very useful in integration scenarios. In addition, these types of scenarios may have pre-existing JavaScript that can be used. Fortunately, Silverlight gives you the flexibility to call JavaScript from managed code.
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}