tags:

views:

120

answers:

3

Hi,

Any ideas on how to pass parameters to Silverlight on startup from URL QueryString?

Thank You

+3  A: 

One approach you can take is to expose a method that can be accessed from JavaScript. So in your xaml.cs file you need to add the following to your constructor:

this.Loaded += new RoutedEventHandler(Page_Loaded);

Then add the following event handler:

void Page_Loaded(object sender, RoutedEventArgs e)
{
    HtmlPage.RegisterScriptableObject("YourControlName", this);
}

and:

[ScriptableMember]
public void YourMethod(string yourData)
{
    // Do your stuff here
}

Then in the ascx or aspx page where your Silverlight control is instantiated add the following JavaScript:

var silverlightControl;

function onSilverlightLoad(sender, args) {
    silverlightControl = sender.getHost();
    var yourData = "some data";
    silverlightControl.Content.YourControlName.YourMethod(yourData);
}

It does also mean that your Silverlight control has to be instantiated via the <object... tag rather than via <asp:Silverlight...

ChrisF
I have multiple SL clients on the page, will this JS pass the data to all the clients?thanks
James
@Jayesh - each Silverlight client instantiated can have it's own onLoad handler so you can call different methods on startup.
ChrisF
+2  A: 

Although Chris's method will work, it's easier to pass startup information through Silverlight's initialization parameters feature.

Ben M
+1  A: 

If all you need to do is get at key-value pairs of the query string, there is a much simpler way using the HtmlPage class:

HtmlPage.Document.QueryString["your_key"];
Dan Auclair