Tip: how to obtain ASP. NET page parameters in a Silverlight Application

Source: Internet
Author: User
Overview

When developing a Silverlight application, we often encounter such a situation that we need to obtain ASP in the UserControl of the Silverlight application. NET page parameter. This parameter may not come from the host page of the current Silverlight application, but may come from other ASP.. NET page.

This document describes how to obtain ASP. NET page parameters in a Silverlight application.

Requirement

The following project structure is displayed in Default. the aspx page needs to pass two values to SilverlightTestPage. the aspx page, which must be in SilverlightTestPage. the following information is obtained and displayed in the Silverlight application hosted by the aspx page:

The Default. aspx page looks as follows:

When you click the "Submit" button, go 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));}

Use InitParams

In the first method, we will naturally use the InitParams attribute. when loading the Silverlight host page, we get the parameters on the ASPX page and pass them to the Silverlight application as the initialization parameters, 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"]);}

Set two attributes in the UserControl of the Silverlight application, as shown in the following code:

public String UserName{    set {        this.lblUserName.Text = value;    }}public String Email{    set {        this.lblEmail.Text = value;    }}

In this way, the initialization parameters can 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;}

In this way, the parameters on the ASP. NET page are obtained in the Silverlight application through InitParams. The results are as follows:

Use HtmlDocument

In fact, the above method may be difficult to obtain parameters on the ASP. NET page. We can use HtmlDocument directly to define the QueryString attribute in HtmlDocument, as shown in the following code:

In this way, we can directly use the HtmlDocument object in Silverlight to obtain the parameters on the current Silverlight application host page, as shown in the following code:

void Page_Loaded(object sender, RoutedEventArgs e){    IDictionary<String, String> paras = HtmlPage.Document.QueryString;    this.lblUserName.Text = paras["username"];    this.lblEmail.Text = paras["email"];}

After running, you can see the same effect as the above:

Summary

This article is very simple and describes how to obtain ASP. NET page parameters in the Silverlight application.

Related Article

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.