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;
    Server.Transfer(ResolveUrl("~/SessionSLTestPage.aspx"));
 }

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)
 {
 InitializeComponent();
 SolidColorBrush brush = new SolidColorBrush(FindColor(stringvalue));
 LayoutRoot1.Background = brush;
 }

Source code can be found here

Reblog this post [with Zemanta]
About these ads

Responses

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

    Thanks

  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.

    Thanks
    Deepak.


Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Categories

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: