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