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> ...

Get Getting Started with Silverlight now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.