Recommended

  • Silverlight 4 Podcast Pack with Tim Heuer
  • Building Modular Silverlight Applications
  • Prism -  10 Things to Know
  • Securing Silverlight Application and WCF Service using ASP.Net Authentication Techniques
  • Model– View – ViewModel in Silverlight
Skip Navigation LinksHome / Tips / View Tip

Tip: How to pass initialize parameters to Silverlight application using ASP.NET 3.5 Silverlight control?

+ Add to SilverlightShow Favorites
6 comments   /   posted by Denislav Savkov on Aug 29, 2008
(6 votes)
Categories: General

If you want to pass initialize parameters to Silverlight application from an HTML/ASPX page, you have to find the place where your Silverlight control is inserted into the page using ASP.NET 3.5 Silverlight control and insert the InitParameters attribute as shown in the code snippet.

HTML

<asp:Silverlight .... InitParameters="param1=value1,param2=value2" />

As you can see you have to insert one new attribute InitParameters into the <asp:Silverlight> element. The syntax for parameters definition is pretty straightforward - you just have to enumerate all parameters and their values separated by commas like this: "parameter1=value1,parameter2=value2,parameter3=value3". Once the initialize parameters have been defined, you can obtain them in your application startup event handler using the InitParams property of the StartupEventArgs like this:

C#

private void Application_Startup( object sender, StartupEventArgs e )
{
    //... Other code here ...
    string param1Value = e.InitParams[ "param1" ];
}

That's it!

 

Share


Comments

Comments RSS RSS
  • RE: Tip: How to pass initialize parameters to Silverlight application using ASP.NET 3.5 Silverlight control?  

    posted by PaulHemmings on Dec 07, 2008 21:03

    But how would I read values from a database control and pass them into the silverlight InitParams?

  • RE: Tip: How to pass initialize parameters to Silverlight application using ASP.NET 3.5 Silverlight control?  

    posted by Alan on Jan 12, 2009 12:04

    And what if you're using Silverlight in the ASP.NET MVC framework?

    I'd like to pass in parameters from data in my model, but I don't want to use the code behind to set them, because in the RC1 of MVC ,ASPX code behind files will be no more !

    If you try to pass in parameters in the ASPX file you get the following error:

     "Server tags cannot contain <% ... %> constructs"

     

  • RE: Tip: How to pass initialize parameters to Silverlight application using ASP.NET 3.5 Silverlight control?  

    posted by Russ Blake on Feb 01, 2009 05:03

     What I need to do is eactly what you have shown, thanks.  But I must also set the parameter from the code-behind of the page that is hosting the Silverlight control.  You have them set in the HTML that invokes the control: this will not work in my case, because the parameter is not known until run time.  I am passing it into the page on the Query String.  I can then retrieve the parameter, that is no problem.  But I can't figure out how to set it into the html that invokes the Silverlight control.  Can you show me how this is done? Thanks!

  • RE: Tip: How to pass initialize parameters to Silverlight application using ASP.NET 3.5 Silverlight control?  

    posted by Enrai on Feb 02, 2009 06:37

    The Silverlight itself can access the Url and get the QueryString, so you don't have to bother with passing it tio the HTML that invokes your Silverlight application. Using the HtmlPage class you are able to access the DOM and the url of the current page:

    string queryParam = HtmlPage.Document.QueryString[<Key>];

    For example you can do the following thing: In the App.xaml.cs add the following code in the Application_Startup handler:

    private void Application_Startup( object sender, StartupEventArgs e )
    {
        this.RootVisual = new Page();
        string queryParam = HtmlPage.Document.QueryString["key"];
        this.Resources.Add( <ResourceKey>, queryParam );
    }

    This way you can access the parameter through the application resources, which means it will be easyly accessible from both the Xaml and the Codebehind. The synthax for accessing the resource is:

    XAML

    <silverlightControl Property={StaticResource ResourceKey} />

    C#

    object parameter = ( ( App )Application.Current).Resources[ <ResourceKey> ];

    That's it! ;)

  • RE: Tip: How to pass initialize parameters to Silverlight application using ASP.NET 3.5 Silverlight control?  

    posted by ange on Mar 18, 2009 14:57
    Is it possible to set the initparameters via javascript?
  • RE: Tip: How to pass initialize parameters to Silverlight application using ASP.NET 3.5 Silverlight control?  

    posted by haikubandiet@yahoo.com on Aug 18, 2009 05:42
    is this anything to do with links i can get through NWO hackers/assholes that cia has been party to?

Add Comment

 
 

   
  
  
   
Please add 3 and 3 and type the answer here: