Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

.NET

Turning on Silverlight in ASP.NET


Silverlight is the cross-browser plug-in that brings the power of Windows Presentation Foundation (WPF) to ASP.NET and even plain HTML pages. Put another way, Silverlight is a tool to create more interactive and rich user interfaces for the Web. It allows you run a WPF application represented by a XAML document in the context of a Web browser in a cross-platform manner. (Note, though, that Silverlight is currently limited to Windows and Mac platforms.)

A Silverlight-enabled ASP.NET page contains a system script file named silverlight.js, which represents the client side portion of the Silverlight engine. That script file is included in the Silverlight SDK. You just copy it somewhere in your site and link it to pages. Some handwritten JavaScript code is required to incorporate Silverlight in an ASP.NET page. If you have an ASP.NET 3.5 page, you can use the following code for a sample page:


<asp:ScriptManager ID="ScriptManager1" runat="server">
    <Scripts>
       <asp:ScriptReference Path="Silverlight.js" />
        <asp:ScriptReference Path="Test.aspx.js" />
    </Scripts>
</asp:ScriptManager>

In older versions of ASP.NET, you can use the plain <script> tag to reference any Silverlight scripts. The Silverlight.js file defines the Sys.Silverlight class that represents the engine. The other script file you see in the code snippet is a script file specific to a page and contains some boilerplate code to initialize the Silverlight plug-in within that page.


function createSilverlightHost()
{  
    Sys.Silverlight.createObject(
        "xaml/test.xaml",                   
        $get("placeholder"),                   
        "SilverlightControl1",          
        {                                
            width:'100',               
            height:'100',              
        },
        {
            onError:null,              
            onLoad:null                
        },
        null);                         
}

As a result of this script, the Silverlight engine is embedded in the host page as an external object through a dynamically generated <object> tag. Where exactly? It will be injected in the host page as a child of the specified placeholder element—normally a <div> tag.

The content of the Silverlight block is fully determined by the referenced XAML document. The XAML document can be a static server resource or a dynamic endpoint that generates XAML content on the fly. What really matters is that you make the Silverlight engine point to a URL that returns XAML contents.

You can programmatically access the contents of the XAML document using JavaScript through the following syntax:


var host = $get("SilverlightControl1");
var element = host.content.findName("TextBlock1");

SilverlightControl1 is simply the ID you assign to the Silverlight block when you create it in the preceding createSilverlightHost function. A Silverlight object has a content property to access its internal tree of objects. Once you have retrieved a given element, you can modify its state and appearance through the XAML object model. Programmable XAML elements have the x:Name attribute set to a unique name. You retrieve these elements passing the name attribute to the findName method.

The elements in the XAML document can be scripted too. For example, in the XAML file you can attach some JavaScript functions to events such as mouse-enter and mouse-leave. JavaScript, however, is the only programming language that can be used to make Silverlight more dynamic.

In the upcoming ASP.NET 3.5 Extensions, Microsoft introduces a new server control—the Silverlight manager—that saves you from writing most of the preceding JavaScript code. All that you have to do is adding this new control to the ASP.NET page and work with its exposed set of properties.

It is essential to note that all the Silverlight capabilities mentioned in this article refers to Silverlight 1.0, released last September. Silverlight 1.0 supports only a small subset of the WPF syntax and is mostly limited to media and graphics capabilities. In a few months, a new version of Silverlight will be out with a largely enhanced set of features. The new version of Silverlight, labeled with a 2.0 version number, will support the full WPF syntax, incorporate a CLR instance and provide support for managed languages and a subset of the .NET Framework.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.