(X) Hide this
    • Login
    • Join
      • Generate New Image
        By clicking 'Register' you accept the terms of use .
        Login with Facebook

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

(11 votes)
Denislav Savkov
>
Denislav Savkov
Joined Feb 11, 2008
Articles:   14
Comments:   6
More Articles
9 comments   /   posted on Aug 29, 2008
Tags:   denislav-savkov
Categories:   Line-of-Business , 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!

 


Subscribe

Comments

  • -_-

    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!

  • Enrai

    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 Muhammad Talha on Feb 08, 2010 11:07
    Great !.Thanks
  • -_-

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


    posted by Ajay on Feb 04, 2011 10:23


    • Great
      1.thanks


  • -_-

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


    posted by Yissel on Feb 07, 2011 22:42

    If those params changed, how can I know it, from the App.xaml.cs

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


    posted by shadiabuhilal on Jun 03, 2011 18:01
    You can use the code to access InitParams in your application 

    Application.Current.Host.InitParams["KeyName"]

Add Comment

Login to comment:
  *      *       
Login with Facebook