Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #37 – How to Dynamically Load and Display Silverlight Applications

Up to now we have been statically declaring our Silverlight application in our web page ASPX file directly like this:

<body style="height:100%;margin:0;">
    <form id="form1" runat="server" style="height:100%;">
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <div  style="height:100%;">
            <asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/SilverlightApplication29.xap" MinimumVersion="2.0.30523" Width="100%" Height="100%" />
        </div>
    </form>
</body>

The purpose of this tip is to show you how to dynamically load and display Silverlight applications. This is useful if you want to switch between Silverlight applications shown on a web page. For our demo purpose, we will create a single page with two Buttons and one Panel. Pushing Button #1 will load the first Silverlight application into the panel. Pushing Button #2 will load a second Silverlight application into the Panel.

image  image

To accomplish this follow these steps:

  1. Create a new Silverlight Application project in Visual Studio 2008. To do this, simply choose File->New Project, Silverlight Application.
  2. Right click on the project node in Solution Explorer, choose Add->New Project, Silverlight Application and link it to the web site. This way you will have two Silverlight applications associated with the web site.
  3. Open default.aspx and add a ScriptManagerPanel, and two Buttons as shown below.
  4. Make default.aspx your default web page by right clicking on it and selecting “Set as Start Page”.
    <body style="height: 100%; margin: 0;">
        <form id="form1" runat="server" style="height: 100%;">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:Panel ID="SilverlightApp" runat="server">
        </asp:Panel>
        <asp:Button ID="Button1" runat="server" Text="Show First App" OnClick="OnShowFirstApp"
            OnCommand="Button1_Command" />
        &nbsp;&nbsp;
        <asp:Button ID="Button2" runat="server" Text="Show Second App" OnClick="OnShowSecondApp"
            OnCommand="Button1_Command" />
        </form>
    </body>
  5. In the code behind in Default.aspx.cs add the to two events as shown below. In each event we are dynamically creating the Silverlight control and setting the Source to point to the XAP file.
    protected void OnShowFirstApp(object sender, EventArgs e)
    {
        System.Web.UI.SilverlightControls.Silverlight sl = new System.Web.UI.SilverlightControls.Silverlight();
     
        sl.Source = "ClientBin/SilverlightApplication1.xap";
        sl.ID = "SilverlightApp1";
        sl.Width = new Unit(800);
        sl.Height = new Unit(600);
        sl.Windowless = true;
        SilverlightApp.Controls.Clear();
        SilverlightApp.Controls.Add(sl);
    }
     
    protected void OnShowSecondApp(object sender, EventArgs e)
    {
        System.Web.UI.SilverlightControls.Silverlight sl = new System.Web.UI.SilverlightControls.Silverlight();
     
        sl.Source = "ClientBin/SilverlightApplication2.xap";
        sl.ID = "SilverlightApp2";
        sl.Width = new Unit(800);
        sl.Height = new Unit(600);
        sl.Windowless = true;
        SilverlightApp.Controls.Clear(); 
        SilverlightApp.Controls.Add(sl);
    }

Looking at the code above you will see we first declare a Silverlight object which is found in the namespace System.Web.UI.SilverlightControls. Once created, we set the Source to point to the location of the Silverlight application’s XAP file. Optionally, we also set an ID, width, height and Windowless = true.

Download this entire project here: Source Code

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Blogs said:

Up to now we have been statically declaring our Silverlight application in our web page ASPX file directly

# September 9, 2008 5:15 PM

2008 September 10 - Links for today « My (almost) Daily Links said:

Pingback from  2008 September 10 - Links for today &laquo; My (almost) Daily Links

# September 10, 2008 2:28 AM

aganariman said:

Hi. I've tried to add silverlight control dynamically to a aspx page that has only scriptmanager on it. The example in your post works but if panel where you put you sl control (SilverlightApp panel) is added dynamically too then silverlight control does not find it's source - xap file when page is opened in browser. Could you try to change your OnShowFirstApp void as follows:

-------------------------------------

protected void OnShowFirstApp(object sender, EventArgs e)

   {

       System.Web.UI.SilverlightControls.Silverlight sl = new System.Web.UI.SilverlightControls.Silverlight();

       sl.Source = "ClientBin/SilverlightApplication1.xap";

       sl.ID = "SilverlightApp1";

       sl.Width = new Unit(400);

       sl.Height = new Unit(300);

       sl.Windowless = true;

       Panel panelForSLControl = new Panel();

       panelForSLControl.ID = "mypanel";

       panelForSLControl.Controls.Add(sl);

       this.Controls.Add(panelForSLControl); //add panel to aspx page

       //SilverlightApp.Controls.Add(sl);

   }

---------------------------------------------

When I run it there is a little difference in source of the rendered page. There is no Sys.Application.add_init part:

//<![CDATA[

Sys.Application.initialize();

Sys.Application.add_init(function() {

   $create(Sys.UI.Silverlight.Control, {"source":"ClientBin/SilverlightApplication2.xap"}, null, null, $get("SilverlightApp1_parent"));

});

//]]>

May be I have a little bug somewhere. Thanks for your posts.

# September 10, 2008 3:07 AM

Silverlight news for September 10, 2008 said:

Pingback from  Silverlight news for September 10, 2008

# September 10, 2008 5:19 AM

aganariman said:

My problem with adding silverlight control to dynamically created Panel is solved!

Problem was because I added Panel to collection of controls of current Page(this.Controls.Add()). But scriptManager instance is in this.form1 control collection. When I add Panel to this.form1.Controls everything is ok.

wrong:

   this.Controls.Add(panelForSLControl);

correct:

   this.form1.Controls.Add(panelForSLControl);

do not do my bug :)

# September 10, 2008 6:40 AM

Dew Drop - September 10, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - September 10, 2008 | Alvin Ashcraft's Morning Dew

# September 10, 2008 9:10 AM

Community Blogs said:

Bart Czernicki on MultiThreading, Cigdem with HangRobot, Corey Schuman with a video slider, Terence Tsang

# September 10, 2008 3:13 PM

samcov said:

I guess this is an alternative pseudo dynamic dll loading method... interesting.

# September 10, 2008 10:21 PM

HoangLeMinh said:

It is need to call method  SilverlightApp.Controls.RemoveAll(); at the top of two functions click

# September 10, 2008 10:57 PM

mike.snow said:

Hoang- Probably a good thing to do :)

I added above: SilverlightApp.Controls.Clear();

# September 10, 2008 11:27 PM

Visual Web Developer Team Blog said:

&#160; Silverlight Tip of the&#160; Day #39 Title : How to Create a Zoom Toolbar Demo: silverlight.services.live.com/.../iframe.html

# September 11, 2008 1:21 AM

Mirrored Blogs said:

Post: Approved at: Sep-13-2008 NBC Did not dump Silverlight! There have been a bunch of posts lately

# September 13, 2008 4:34 AM

Bryant Likes's Blog said:

qingquan126778 asked the question in the Silverlight forums about how to switch between pages in Silverlight

# December 22, 2008 11:16 PM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight tips of the

# January 2, 2009 5:56 PM

o UAU nosso de cada dia said:

essa lista eu copiei desse blog bárbaro (acompanhe por RSS você também): uma lista de dicas super úteis

# January 3, 2009 6:25 AM

Silverlight Travel » Switching XAP Files on the Client Side said:

Pingback from  Silverlight Travel &raquo; Switching XAP Files on the Client Side

# January 24, 2009 4:18 AM

LetsBlogAbout.NET said:

Silverlight Tips of the Day

# October 15, 2010 11:08 AM