Cheap Windows Hosting tutorial – How to pass parameters to Silverlight controls from ASP.NET pages ?

ByAlexia Pamelov

Cheap Windows Hosting tutorial – How to pass parameters to Silverlight controls from ASP.NET pages ?

CheapWindowsHosting.com | In this post we will explain you about How to pass parameters to Silverlight controls from ASP.NET pages ? You can pass parameters from your aspx pages and html pages to the Silverlight controls. This chapter explains how to pass parameters to Silverlight controls from your aspx page and code behind files.

CustomerService-

InitParameters

The Xaml page user control has a property called InitParameters. You can set a value in the form of key-value pairs from your ASPX pages. Since this property accepts key-value pairs, you can pass any set of string values.

How to set InitParameters

Example – Set InitParameters property from ASPX page:

<asp:Silverlight 
   ID="Xaml1" 
   runat="server" 
   Source="~/ClientBin/MySilverlightApp.xap" 
   InitParameters="City=Houston,State=Texas,Country=USA"
   Width="300" 
   Height="300" />

You can set this property from the code behind file of your ASPX page as well.
Example – Set InitParameters property from the code behind file:

Xaml1.InitParameters = "City=Houston,State=Texas,Country=USA";

How to retrieve the InitParameters value ?

The value you pass to a Silverlight control through the InitParameters property can be retrieved from the Application_Startup even handler in the App.xaml page.

private void Application_Startup(object sender, StartupEventArgs e)
{
    IDictionary<string, string=""> parameters = e.InitParams;
    this.RootVisual = new Page1();

Now, in most cases, you may want to pass this value to the xaml page itself instead of doing anything with in it the App.xaml.

Pass parameters from App.xaml to other pages

To pass the parameters from the App.xaml pages to other pages, you must modify the constructor of xaml page class to accept parameters.

private IDictionary<string, string> parameters = null;
public Page1()
{
    InitializeComponent();
}
public Page1(IDictionary<string, string> p)
{
    this.parameters = p;
    InitializeComponent();
}

The above sample shows an additional constructor added which takes a parameter of type IDictionary and then sets the value to a member variable.

Now go back to your App.xaml and pass the parameters to the page:

private void Application_Startup(object sender, StartupEventArgs e)
{
    IDictionary<string, string=""> parameters = e.InitParams;
    this.RootVisual = new Page1(parameters);

Use the IDictionary parameter values in the XAML pages

If you have followed the above steps correctly, you should be able to access the IDictionary values in your XAML page.

textblock1.Text = this.parameters["City"];

Here is the complete code from the XAML page:

<UserControl x:Class="MySilverlightApp.Page1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300" Loaded="UserControl_Loaded">
    <Grid x:Name="LayoutRoot" Background="White">
<TextBlock x:Name="textblock1" Width="200" Height="30"></TextBlock>
    </Grid>
</UserControl>

In the code behind file for your page1.xaml, you can set the Text for the textblock1 control as shown below:

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
        textblock1.Text = parameters["City"];

  Best OFFER Cheap SilverLight 6 Hosting ! Click Here

 

 

Simpan

Simpan

About the Author

Alexia Pamelov administrator

    Leave a Reply