ASP.NET and Silverlight

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.

Commingling with ASP.NET

In the most basic case of integration between Silverlight and ASP.NET, you will want 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 shown in Example E-15.

Example E-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/silverlight.js"></script>
</head>
<body>
  <form id="form1" runat="server">
    <div>
      <h3>ASP.NET and Silverlight</h3>
      <p><asp:Button ID="clickMe" runat="server" Text="Click me!" /></p>
    </div>
    <div id="agContainer">
      <script type="text/JavaScript">
        Sys.Silverlight.createObject(
          "xaml/plugin.xaml",
          document.getElementById("agContainer"),
          "theHost",
          { width: "200", height: "200", version: "0.9"},
          {});
      </script>
    </div>
  </form>
</body>
</html>

The example shows a simple XAML document on an ASP.NET web page alongside HTML and server-side content. Much as you used the createFromXaml method of the Silverlight host ...

Get Programming WPF, 2nd Edition 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.