Posted by: dotnetninja | October 8, 2008

Passing data from ASP.NET page to Silverlight Application

In some cases maybe you want to initialize Silverlight application by passing some parameters to it.

There is several possible ways to do it. I will demonstrate one of the simplest.

Here what the application may look like. An ASP.NET page passing some value to page where Silverlight application is defined.

Startup page will contain one DropDownList with colors and one button to navigate to Silverlight page. User should choose one color press button and set background of the Grid in the Silverlight to that selected Color.

When user clicks Button following code will execute (on the Default.aspx).

 protected void Button1_Click(object sender, EventArgs e)
    Session["Color"] = DropDownList1.SelectedValue;

On the SessionSLTestPage.aspx we have code to initialize Silverlight:

protected void Page_Load(object sender, EventArgs e)
 Xaml1.InitParameters = "mySession=" + Session["Color"];

In the Silverlight project change App.xaml.cs

private void Application_Startup(object sender, StartupEventArgs e)
  string session1 = e.InitParams["mySession"];
  this.RootVisual = new Page(session1);

and finally on the page add new constructor to be able to pass value.

public Page(string stringvalue)
 SolidColorBrush brush = new SolidColorBrush(FindColor(stringvalue));
 LayoutRoot1.Background = brush;

Source code can be found here

Reblog this post [with Zemanta]


  1. Very nice article. This is what I want exactly..


  2. hi.. can i know how to put that theme behind the source codes. i mean using that we can identify source code separately.. i want to use it in my blog

  3. Thanks for sharing.
    Do you know how to pass data from silverlight app to an aspx page?
    please share.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

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

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s


%d bloggers like this: