How to pass values to Silverlight application from ASPX page using InitParameter?



Neither the Silverlight is new nor the init params. But, I found various people in different forums asking for the logic on the implementation part for passing value to Silverlight application from ASP.Net application. In this post, I will describe about it.

 

Here I will discuss on the InitParams and tell you how to use it to pass value from your .aspx page to your Silverlight application. Read the complete post and if you have any queries, don’t hesitate to ask me.

 

InitParams is one of the param name of the Silverlight plug-in object. Generally it is a dictionary object of type string. It contains a set of user defined key/value pairs. By default it is set as null or empty string and initializes at the time of first instance. Later it becomes readonly and hence you will not be able to modify it. If you have more than one string values to pass, use comma separated key/value pairs. Have a look into it:

 

<param name="initparams" value="a=10,b=20" />

 

Here, I am passing 10 & 20 as two string value to the key names a & b respectively. Now you can retrieve the value in backend code by using the Key a & Key b. I will discuss it later part.

 

Let us discuss about it in more depth. Your Silverlight application hosts in either HTML or ASPX page. In that page you will find the following code snippet where you have an <object></object> tag. There are several <param /> tags inside it. Create one more param tag as mentioned above and set the value to that. See the code here:

 

image

 

Now, it is time to retrieve the code from your .cs file. Open your App.xaml.cs file. There find the event named “Application_Startup”. This has a parameter called “StartupEventArgs” and contains property named “InitParams”. This will have the values you entered in the HTML/ASPX page.

 

image

Once retrieved, you can iterate through each key/value pair to read the values. Have a look into the below code:

 

image


In the above example, I read each of the initparam values and shown with the help of MessageBox. So, what next? Now you can read the value and send it to your RootVisual UserControl/Page (i.e. MainPage in this example) by creating a parameterized constructor for the MainPage. There might be a question “Do I have to read the values by iterating each key inside the App.xaml.cs and then have to send it to my Silverlight page?” Nope, not the case. You can send the whole dictionary to your MainPage and then you can iterate through each key.

 

Hope, now you got the basic idea on that & can now read any values from your .aspx/.html page in your Silverlight application. But remember that, you can only set this values once you are initializing the application. After the initialization is done, “initparams” becomes readonly and you will not be able to set/modify it anymore.

6 comments

  1. I have posted a similar blog on http://www.pochet.net/blog/2010/06/09/passing-parameters-to-a-silverlight-xap-application/ in which also passing parameters using a querystring to the containing ASPX page is discussed.

    ReplyDelete
  2. Hi Emiel,

    Thanks for sharing your blog entry here. It's nice one.

    ReplyDelete
  3. On our projects, we've been using InitParams for quite a while. We do it using a dynamically generated string though. So in default.aspx, you have:

    <param name="InitParams" value="<%InitParams%>" />

    Then in default.aspx.cs:

    public string InitParams {
    get { return InitParameters.GetValues(this); }
    }

    Then we have the static InitParameters class generate the values we need.

    The main benefit for us is that has allowed us access to the settings in web.config. This means it's very simple to change things such as database connection strings, logging levels, etc.

    ReplyDelete
  4. Hi notahelpdesk,

    You are absolutely correct and the approach you are following there is 100% perfect. In static way, you have to follow the approach I gave but in dynamic way, means when you want to read it from config, you must have to use the <% ... %> directive.

    Without that, you can't read any value in your Silverlight application from your .aspx page.

    Only thing is: you have to initialize before the .XAP file loads.

    Cheers... :)

    Regards,
    Kunal

    ReplyDelete
  5. hi kunal i m developing an application in silverlight to plot graphs which wil be used as widget , here i need to pass an ID(integer) from the HTMl r aspx page , n then i need to collect that in the mainpage.xaml.cs and few more pages n then use that ID to get the data , how can i collect the ID in the main page.xaml.cs....
    i came to know how to send the ID from HTML n aspx but the main problem is in receiving it in the mainpage.xaml.cs . i got the ID in App.xaml.cs but i m struggling in finding it in the mainpage.xaml.cs . kindly help me out .....

    thanks in advance
    Anil

    ReplyDelete
  6. hey kunal....can u help me with using asp.net 3.5 with silverlight. We need to build an application where in we need to draw dynamic lines betwwen two images. Please help and database is aslo used. So please help

    ReplyDelete


 
© 2008-2014 Kunal-Chowdhury.com | Designed by Kunal Chowdhury
Back to top