Silverlight Tutorial - Interaction With The DOM
| The example below might look familiar - visually, it looks identical to the example in our Silverlight And Javascript Interaction tutorial. The code behind it, though, is completely different this time. Whereas last time javascript did all the heavy lifiting - telling the Silverlight app when to change, changing the DOM when Silverlight needed it changed - this time we completely eliminated all that from the picture. |
When you click on a button on the left, Silverlight modifies the DOM directly to add the text on the right. When you click a button on the left, the Silverlight app actually receives an
onclick from the html element, and responds appropriately.|
|
HTML/CSS
Click one of the buttons below to update the Silverlight.
|
So how do we do it? Well, it is actually pretty simple. First, lets take a look at how we change the text on the html side from a click on the Silverlight side. Assuming you have the
onclick events hooked up to the following function in the xaml code, it is as simple as this:private void SilverlightButtonClicked(object o, MouseEventArgs e)
{
UIElement uio = o as UIElement;
HtmlElement msgBox =
HtmlPage.Document.GetElementByID("SilverlightMessage");
if(uio != null && msgBox != null)
msgBox.SetProperty("innerHTML", "Silverlight Button "
+ uio.Name.Substring(0,1) + " Clicked");
}
{
UIElement uio = o as UIElement;
HtmlElement msgBox =
HtmlPage.Document.GetElementByID("SilverlightMessage");
if(uio != null && msgBox != null)
msgBox.SetProperty("innerHTML", "Silverlight Button "
+ uio.Name.Substring(0,1) + " Clicked");
}
I gave my Silverlight buttons different names ("ABox", "BBox", "CBox"), which is why I cast the object to a
UIElement (so that I can access the name property). But it is the next line that is the most important. Here, we actually get the div that we want to put text into. The HTMLPage is a static object that gives you all sorts of information about the page and the browser. It is contained in the System.Windows.Browser namespace, so you will want to add that as a using statement at the top of your code file. Once we have the HtmlPage, we want the get the Document, which, surprise surprise, is extremely similar to the document object that you might access in javascript - right down to the GetElementById function, which we use here to get the element.Html page elements all derive from the class
HtmlElement, which is what GetElementById returns. So once we have a reference to an element, what can we do with it? Well, pretty much anything you would be able to in javascript. Often the syntax is different, because javascript is weakly typed and has soft objects - very different from C# - but the names of everything are extremely similar. Here we want to set the content of this "SilverlightMessage" div, so all we do is call SetProperty on the innerHTML property, and give it the new value.So what about the other direction - Silverlight apps attaching to DOM events? Well, that works too, and again should be familiar to anyone who has written event code in javascript:
private void HookEvents()
{
HtmlElement button =
HtmlPage.Document.GetElementByID("HTMLButtonA");
if (button != null)
button.AttachEvent("onclick",
new EventHandler<HtmlEventArgs>(
delegate(object o, HtmlEventArgs e) {
HtmlButtonClicked("A");
}));
button =
HtmlPage.Document.GetElementByID("HTMLButtonB");
if (button != null)
button.AttachEvent("onclick",
new EventHandler<HtmlEventArgs>(
delegate(object o, HtmlEventArgs e) {
HtmlButtonClicked("B");
}));
button =
HtmlPage.Document.GetElementByID("HTMLButtonC");
if (button != null)
button.AttachEvent("onclick",
new EventHandler<HtmlEventArgs>(
delegate(object o, HtmlEventArgs e){
HtmlButtonClicked("C");
}));
}
private void HtmlButtonClicked(string which)
{
HTMLMessage.Text = "HTML Button " + which + " Clicked";
}
{
HtmlElement button =
HtmlPage.Document.GetElementByID("HTMLButtonA");
if (button != null)
button.AttachEvent("onclick",
new EventHandler<HtmlEventArgs>(
delegate(object o, HtmlEventArgs e) {
HtmlButtonClicked("A");
}));
button =
HtmlPage.Document.GetElementByID("HTMLButtonB");
if (button != null)
button.AttachEvent("onclick",
new EventHandler<HtmlEventArgs>(
delegate(object o, HtmlEventArgs e) {
HtmlButtonClicked("B");
}));
button =
HtmlPage.Document.GetElementByID("HTMLButtonC");
if (button != null)
button.AttachEvent("onclick",
new EventHandler<HtmlEventArgs>(
delegate(object o, HtmlEventArgs e){
HtmlButtonClicked("C");
}));
}
private void HtmlButtonClicked(string which)
{
HTMLMessage.Text = "HTML Button " + which + " Clicked";
}
So we again call
GetElementByID in order to get our hands on the html elements. Next, to attach an event, we just call the function AttachEvent - which is extremely similar to the Internet Explorer standard for attaching events in javascript. The major difference is that in C# we have to hand the function an EventHandler, while in javascript we can just hand it a function. But that is just a byproduct of the syntax differences between the two languages.
Here, we are attaching a
delegate we are creating on the fly, and all that delegate does is call the function HtmlButtonClicked with the right argument. And that function just sets the text of a TextArea block on the Silverlight app.And that is all you need to do! Below is the entire content of the code behind file:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
namespace SilverlightProject2
{
public partial class Page : Canvas
{
public void Page_Loaded(object o, EventArgs e)
{
this.InitializeComponent();
HookEvents();
}
private void HookEvents()
{
HtmlElement button =
HtmlPage.Document.GetElementByID("HTMLButtonA");
if (button != null)
button.AttachEvent("onclick",
new EventHandler<HtmlEventArgs>(
delegate(object o, HtmlEventArgs e) {
HtmlButtonClicked("A");
}));
button =
HtmlPage.Document.GetElementByID("HTMLButtonB");
if (button != null)
button.AttachEvent("onclick",
new EventHandler<HtmlEventArgs>(
delegate(object o, HtmlEventArgs e) {
HtmlButtonClicked("B");
}));
button =
HtmlPage.Document.GetElementByID("HTMLButtonC");
if (button != null)
button.AttachEvent("onclick",
new EventHandler<HtmlEventArgs>(
delegate(object o, HtmlEventArgs e){
HtmlButtonClicked("C");
}));
}
private void HtmlButtonClicked(string which)
{
HTMLMessage.Text = "HTML Button " + which + " Clicked";
}
private void SilverlightButtonClicked(object o,
MouseEventArgs e)
{
UIElement uio = o as UIElement;
HtmlElement msgBox =
HtmlPage.Document.GetElementByID("SilverlightMessage");
if(uio != null && msgBox != null)
msgBox.SetProperty("innerHTML", "Silverlight Button "
+ uio.Name.Substring(0,1) + " Clicked");
}
}
}
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
namespace SilverlightProject2
{
public partial class Page : Canvas
{
public void Page_Loaded(object o, EventArgs e)
{
this.InitializeComponent();
HookEvents();
}
private void HookEvents()
{
HtmlElement button =
HtmlPage.Document.GetElementByID("HTMLButtonA");
if (button != null)
button.AttachEvent("onclick",
new EventHandler<HtmlEventArgs>(
delegate(object o, HtmlEventArgs e) {
HtmlButtonClicked("A");
}));
button =
HtmlPage.Document.GetElementByID("HTMLButtonB");
if (button != null)
button.AttachEvent("onclick",
new EventHandler<HtmlEventArgs>(
delegate(object o, HtmlEventArgs e) {
HtmlButtonClicked("B");
}));
button =
HtmlPage.Document.GetElementByID("HTMLButtonC");
if (button != null)
button.AttachEvent("onclick",
new EventHandler<HtmlEventArgs>(
delegate(object o, HtmlEventArgs e){
HtmlButtonClicked("C");
}));
}
private void HtmlButtonClicked(string which)
{
HTMLMessage.Text = "HTML Button " + which + " Clicked";
}
private void SilverlightButtonClicked(object o,
MouseEventArgs e)
{
UIElement uio = o as UIElement;
HtmlElement msgBox =
HtmlPage.Document.GetElementByID("SilverlightMessage");
if(uio != null && msgBox != null)
msgBox.SetProperty("innerHTML", "Silverlight Button "
+ uio.Name.Substring(0,1) + " Clicked");
}
}
}
Look for some performance testing in the future, where we see how using Silverlight to modify the DOM stacks up against plain old javscript! Until then, if you have any questions or comments, feel free to leave them below.
Posted in Silverlight, All Tutorials by The Tallest |

September 25th, 2007 at 11:44 pm
You might want to look at jQuery. It’s A LOT better.
September 26th, 2007 at 5:12 am
Thanks for tutorial.
I wanna ask you one thing..
Is it convince to learn about SliverLight for those who are not familiar with ASP.NET Ajax?
Is ASP.NET Ajax the main key to learn Sliverlight?
September 26th, 2007 at 8:28 am
Tom:
jQuery is pretty cool, but it serves an entirely different purpose than silverlight. Obviously, you wouldn’t create a silverlight app whose sole purpose was to manipulate the DOM - for that you would use jQuery or just plain old javascript. This tutorial is just to show that silverlight can in fact interact with the DOM when you need it to.
Michael:
None of us here at Switch On The Code actually know ASP or ASP.NET, so I don’t think having ASP experience is needed to learn how to work with silverlight.
September 26th, 2007 at 11:09 am
Michael, ASP.NET and Silverlight, both being Microsoft products, have some pretty nice integration - like web services. However, everything that can be done using ASP.NET can also be done using any other server side language, like PHP, Ruby, or Python. Programming with Silverlight doesn’t depend on any prior ASP.NET knowledge. As an example of not using ASP.NET as the server side language, we’ll be posting a tutorial in the near future discussing communication between Silverlight and PHP.