Several methods for transferring values between ASP. NET pages

Source: Internet
Author: User

ASP. net web forms provides developers with an excellent event-driven development mode. However, this simple application development mode brings us some small problems. For example, in the traditional ASP application, you can easily transfer one or more values from one page to another through the POST method, and use the same method in ASP. NET. Here, we can solve this problem in other ways. ASP. NET provides three methods for us. One is to use QueryString to transmit the corresponding value, the other is to transmit the corresponding value through the session variable, and the other is to use the Server. the Transfer method. The following sections describe one by one:

I. Querystring
Querystring is a simple value transfer method. Its disadvantage is that it displays the value to be transferred in the address bar of the browser and cannot pass objects in this method. If you want to pass a security that is not so important or a simple value, it is best to use this method. The following is a small example to complete the data transfer process. The procedure is as follows:
1. Create a web form
2. Place a button1 in the new web form, and place two TextBox1 and TextBox2
3. Create a click event for the button
The Code is as follows:
Private void button#click
(Object sender, System. EventArgs e)
{
String url;
Url = "webform2.aspx? Name = "+
TextBox1.Text + "& email =" +
TextBox2.Text;
Response. Redirect (url );
}
4. Create a new target page named webform2
5. Place Label1 and Label2 in webform2.
Add the following code to Page_Load of webform2:
Private void Page_Load
(Object sender, System. EventArgs e)
{
Label1.Text = Request. QueryString ["name"];
Label2.Text = Request. QueryString ["email"];
}
Run the command to view the passed result.

Ii. Use Session Variables

Using Session variables to pass values is the most common method. In this method, values can be transmitted not only to the next page, but also to multiple pages, the variable does not disappear until the value of the Session variable is removed. For example:
1. Create a web form
2. Place a button1 in the new web form, and place two TextBox1 and TextBox2
3. Create a click event for the button
The Code is as follows:
Private void button#click
(Object sender, System. EventArgs e)
{
Session ["name"] = TextBox1.Text;
Session ["email"] = TextBox2.Text;
Response. Redirect ("webform2.aspx ");
}
4. Create a new target page named webform2
5. Place Label1 and Label2 in webform2.
Add the following code to Page_Load of webform2:
Private void Page_Load
(Object sender, System. EventArgs e)
{
Label1.Text = Session ["name"]. ToString ();
Label2.Text = Session ["email"]. ToString ();
Session. Remove ("name ");
Session. Remove ("email ");
}
Run the command to view the passed result.

Iii. Use Server. Transfer
Although this method is a bit complicated, it is also a way to pass values on the page.
For example:
1. Create a web form
2. Place a button1 in the new web form, and place two TextBox1 and TextBox2
3. Create a click event for the button
The Code is as follows:
Private void button#click
(Object sender, System. EventArgs e)
{
Server. Transfer ("webform2.aspx ");
}
4. The TextBox1 is returned during the creation process. The value code of the TextBox2 control is as follows:
Public string Name
{
Get
{
Return TextBox1.Text;
}
}

Public string EMail
{
Get
{
Return TextBox2.Text;
}
}
5. Create a new target page named webform2
6. Place Label1 and Label2 in webform2.
Add the following code to Page_Load of webform2:
Private void Page_Load
(Object sender, System. EventArgs e)
{
// Create an instance of the original form
WebForm1 wf1;
// Obtain the instantiated handle
Wf1 = (WebForm1) Context. Handler;
Label1.Text = wf1.Name;
Label2.Text = wf1.EMail;

}
Run the command to view the passed result.

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.