[Code] Get the control value of the Source Page

Source: Internet
Author: User

Summary
In ASP. NET, you can use either of the following methods to obtain the data on the Source Page:
First, obtain the property value of the control by obtaining the control on the Source Page.
The second is to directly obtain the Source Page data by obtaining the public attributes published on the Source Page.

This sample code demonstrates how to obtain the Source Page data by obtaining the Source Page Control.
There are two cases: one is to obtain the control on the Source Page, and the other is to obtain the control in the Source Page.
In either case, you must use the page. previouspage attribute of the target page to obtain the reference of the Source Page.
Then, you can call the findcontrol () method of the page to obtain the control on the page, or call the findcontrol () method of the control to obtain the control's internal control.
It should be noted that, whether you get the Source Page reference through the page. previouspage attribute or find the required control through findcontrol (), null may occur.
Therefore, before using these objects, make sure they are not null. Otherwise, errors will certainly occur.

1. Get the control on the Source Page

Sample Code
In this sample code, there is a Textbox Control named usernametextbox on the Source Page. You can enter your own name in this control and click Submit to target page, the source page will be sent to the destination Page destinationpage. aspx. In the code of the target page. the previouspage property gets the reference from the source page, and then finds the usernametextbox control through findcontrol (), and then obtains the name entered on the Source Page through the text property of this control. The Code is as follows:
Source Page code

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "sourcepage1.aspx. cs" inherits = "sourcepage" %> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml"> 

The following code retrieves the Source Page data on the target page:

if (this.PreviousPage != null){    TextBox UserNameTextBox =        (TextBox)this.PreviousPage.FindControl("UserNameTextBox");    if (UserNameTextBox != null)    {        this.Response.Write(string.Format(            "{0}, Welcome you to the ASP.NET 4 World!",            UserNameTextBox.Text));    }}

Test the above Code and you will find that you have obtained the user name entered on the source page from the target page correctly. Pai_^

2. Obtain the control in the Source Page Control.

KEY NOTES
This situation is a little more difficult than the previous situation, but it is not as daunting as it is.
To put it simply, the container control that contains the control is obtained first, and then the required control is obtained through the findcontrol () method of the Container Control.
What if the control is in the Container Control in the container control?
The truth is the same. Continue to find the Container Control in the container control until you find the upper-level container control that contains the control and then findcontrol () It.

Sample Code
In this example, we want to find the text box control used to enter the user name in the login control of the page. The ID of this control is username. First, we can find the login control by calling page. previouspage. findcontrol (), and then call login. findcontrol () to find the text box control.
The Code on the Source Page is as follows:

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "sourcepage2.aspx. cs" inherits = "sourcepage2" %> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml"> 

The code for obtaining the user name on the target page is as follows:

if (this.PreviousPage != null){    Login UserLogin = (Login)this.PreviousPage.FindControl("UserLogin");    if (UserLogin != null)    {        TextBox UserNameTextBox = (TextBox)UserLogin.FindControl("UserName");        if (UserNameTextBox != null)        {            this.Response.Write(string.Format(                "{0}, Welcome you to the ASP.NET 4 World!",                UserNameTextBox.Text));        }    }}

Test to see if the user name on the Source Page has been obtained correctly?

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.