ASPNET Controls That Use Silverlight

As you saw in Chapter 1, it's easy to build a simple ASRNET web application that includes Silverlight content. You simply need to create a website that includes an HTML or an .aspx test page.

Although this approach allows you to place Silverlight and ASRNET pages side by side on the same website, they aren't in any way integrated. You can navigate from one page to another (for example, use a link to send a user from an ASRNET web form to a Silverlight entry page), but there's no interaction between the server-side and client-side code. In many situations, this design is completely reasonable, because the Silverlight application represents a distinct "applet" that's available in your website. In other scenarios, you might want to share part of your data model, or integrate server-side processing and client-side processing as part of a single task. This requires more planning.

The simplest type of interaction is for an ASRNET control to generate some Silverlight content. In fact, this approach suits the Silverlight model quite well. Silverlight content is wrapped into a distinct element (usually, it's an <object> element that's placed in a <div>), which can coexist alongside other content. An ASRNET control could render the markup for the <object> element in the same way it spits out simple HTML for standard web controls (like the Button) or a combination of HTML and JavaScript for more complex controls (like the Calendar and GridView). In fact, this is exactly how ASRNET's new Silverlight controls work.

These controls are a part of the System.Web.Silverlight.dll assembly, which is part of the Silverlight 2 SDK. When you create a Silverlight and ASRNET solution, a reference is automatically included for this assembly. But in order to use these controls in your pages, you need to register a control tag prefix for the System.Web.UI.SilverlightControls namespace (which is where the Silverlight controls are located). Here's the Register directive that you can add to a web page (just after the Rage directive) to use the standard asp tag prefix with the new ASRNET Futures controls:

<%@ Register Assembly="System.Web.Silverlight" Namespace="System.Web.UI.SilverlightControls" TagPrefix="asp" %>

This directive is automatically inserted in the .aspx test page (for example, Silverlight-ApplicationlTestRage.aspx). It's also added to other web pages when you drag and drop a Silverlight control (from the Silverlight tab of the Toolbox) onto your web form.

Alternatively, you can register the control prefix in your web.config file so that it automatically applies to all pages:

<?xml version="1.0"?> <configuration>

<add tagPrefix="asp" namespace=" System.Web.UI.SilverlightControls" assembly="System.Web.Silverlight" />

</system.web> </configuration>

Currently, ASRNET includes just two Silverlight controls, which are named Silverlight and MediaPlayer. They appear in the Toolbox tab named Silverlight Controls.

Note To use the Silverlight controls, you also need an instance of the invisible ScriptManager control. The ScriptManager powers client-side JavaScript features—for example, it makes the createObject() function available so the Silverlight content region can be created. The ScriptManager control is automatically added to the .aspx test page, but if you create additional ASP.NET web pages that show Silverlight content, you'll need to add the ScriptManager from the Toolbox.

0 0

Post a comment

  • Receive news updates via email from this site