Create a custom web part to render silverlight application

In my previous post, I’ve discussed about how to leverage the content editor web part to render the silverlight application. In this post, I’d be discussing the steps to create a custom web part to render the silverlight application. The custom web part that renders the silverlight application has more advantages. Because things like attributes of silverlight application can be externalized as web part properties. Moreover security can be applied while rendering silverlight application, because the custom web part has power of code-behind support, rather than content editor web part. The whole idea is to render the silverlight control programmatically inside the web parts(using createchildcontrols method) . Here are the steps mentioned below :-

a)Import required namespace

Add a reference to System.Web.Silverlight.

using System.Web.UI.SilverlightControls; 

b)Programmatically add the script manager control to the page

To render the silverlight control, the ScriptManager object needs to added to the Page. Override the OnLoad method of web part, add the ScriptManager control using the following code-snippet. There needs to be only one instance of ScriptManager object registered with the Page object.

    protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            //get the script manager associated with the current page
            ScriptManager oscriptManager =
                  ScriptManager.GetCurrent(this.Page);

            //check whether the script manager is already registered to the page
            if (oscriptManager == null)
            {
                oscriptManager = new ScriptManager();
                Controls.AddAt(0, scriptManager);
            }

        }

 

c)CreateChildControls method to render silverlight application

Override the CreateChildControls method and render the Silverlight control.

 protected override void CreateChildControls()
        {
            base.CreateChildControls();

            Silverlight oSilverlight = new Silverlight();
            oSilverlight.ID = “SLWPID”;
            oSilverlight.Source = “∼/ClientBin/YourSilverlightApp.xap”;
            oSilverlight.Width = Unit.Percentage(100);
            Controls.Add(oSilverlight);
        }

 Subscribe to my post

One thought on “Create a custom web part to render silverlight application

  1. Pingback: Create a custom web part to render silverlight application - My experiments with SharePoint, Azure and .NET using Visual Studio

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.