O'Reilly logo

Getting Started with Silverlight by Shawn Wildermuth

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using Silverlight with ASP.NET

Hosting Silverlight content in a plain HTML file is an interesting exercise, but most .NET web developers will want to be able to integrate Silverlight with their ASP.NET projects. Have no fear: Silverlight and ASP.NET work well together.

Comingling Silverlight with ASP.NET

In the most basic case of integration between Silverlight and ASP.NET, you will want to be able to add Silverlight content to existing .aspx pages. Because ASP.NET is a server-side technology and Silverlight is client-side, you can simply add the Silverlight to the markup of any .aspx page, as seen in Example 15.

Example 15. Silverlight on an ASP.NET page

<%@ Page Language="C#"
         AutoEventWireup="true"
         CodeFile="Default.aspx.cs"
         Inherits="_Default" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
  <title>ASP.NET and Silverlight - Together!</title>
  <script type="text/javascript" src="js/aghost.js"></script>
</head>
<body>
  <form id="form1" runat="server">
    <div>
      <h3>ASP.NET and Silverlight</h3>
      <p>Here is some typical HTML content.</p>
      <p><asp:Button ID="clickMe" runat="server" Text="Click me!" /></p>
    </div>
    <div id="agContainer">
      <script type="text/javascript">
        new AGHost("agContainer",    // hostElementID (e.g. name of the DIV)
                   "theHost",          // ID of the Silverlight Host
                   "200",              // Width
                   "200",              // Height
                   "white",            // Background color
                   null,               // SourceElement
                   "xaml/plugin.xaml", // Source file
                   "false",            // IsWindowless
                   "30",               // MaxFrameRate
                   null                // OnError handler
                  );
      </script> ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required