How to get ASP. NET page parameters in a Silverlight application

Source: Internet
Author: User
Tags silverlight

Get vector aspx page parameters in an ASP. NET Silverlight Application

Sometimes the SL application needs to use the parameter values passed in the ASPX page, and there are usually two ways to get

1. Use the Initparameters property to dynamically set the pass parameters.

2. Use the HtmlPage class in SL engineering to get the page parameters directly.

As the following project structure, We will need to pass two values to the Silverlighttestpage.aspx page on the Default.aspx page and need to be fetched and displayed in the Silverlight application hosted on the Silverlighttestpage.aspx page:

Where the Default.aspx page looks like the following:

When you click the Submit button, turn to the silverlighttestpage.aspx page and pass two parameters, as shown in the following code:

protected void btnSubmit_Click (object sender, EventArgs e)
{
Response.Redirect (String.Format (Silverlighttestpage.aspx?username={0}email={1},
This.txtUserName.Text,
This.txtEmail.Text));
}

Using InitParams

The first way, we naturally use the InitParams property, when loading on the Silverlight host page, we get the parameters in the ASPX page and pass them as initialization parameters to the Silverlight application, as shown in the following code:

protected void Page_Load (object sender, EventArgs e)
{
This. Xaml1.initparameters = String.Format (Username={0},email={1},
Request.querystring[username],
Request.querystring[email]);
}

Then set two properties in the Silverlight application's UserControl, as shown in the following code:

Public String UserName
{
set {
This.lblUserName.Text = value;
}
}
Public String Email
{
set {
This.lblEmail.Text = value;
}
}

This allows the initialization parameters to be obtained in the Application_Startup event and passed to UserControl, as shown in the following code:

private void Application_Startup (object sender, StartupEventArgs e)
{
Page page = new page ();
Page. UserName = E.initparams[username];
Page. Email = E.initparams[email];
This. rootvisual = page;
}

This allows us to get the parameters of the ASP. NET page in the Silverlight application via InitParams

Second approach: Using HTMLDocument

In fact, the above method gets some trouble with the parameters of the ASP. We can use HTMLDocument directly and define the QueryString property in HTMLDocument, as shown in the following code:

This allows us to use the HTMLDocument object directly in Silverlight to get the parameters in the current Silverlight Application Host page, as shown in the following code:

Here's the code to get the parameters using the HtmlPage class:

The code is as follows:

void Page_loaded (object sender, RoutedEventArgs e) {
Idictionary<string, string> paras = HtmlPage.Document.QueryString;
This.lblUserName.Text = paras["username"];
This.lblEmail.Text = paras["email"];}

How to get ASP. NET page parameters in a Silverlight application

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.