asp.net WebForm The method of passing values between pages

Source: Internet
Author: User
asp.net|web| page

asp.net WEB FORMS provides developers with an excellent event-driven development model. However, this simple application development model brings us some minor problems, for example, in a traditional ASP application, you can easily transfer one value or multiple values from one page to another through the Post method (Request ()/request.form ()/ Request.QueryString ()), in the same way in the asp.net to achieve a bit of trouble. Here, there are other ways to solve this situation. Asp. NET provides us with three kinds of ways, one is to transmit the corresponding value by using the QueryString, one kind is to transmit the corresponding value through the session variable, and is realizes through the Server.Transfer method.

First, the use of QueryString

QueryString is a very simple way of passing values, the disadvantage of which is to display the value to be transferred in the browser's address bar, and in this method can not pass the object. It's best to use this method if you want to pass a security that is not so important or a simple value. Here is a small example to complete the work of the transfer, the following steps:
1. Create a Web Form
2. Place a button1 in the new Web form, placing two Textbox1,textbox2
3. Create Click events for button buttons
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, a new target page named WebForm2
5. Place two Label1,label2 in WebForm2
Add the following code to the Page_Load of WebForm2:
private void Page_Load (Object Sender,system.eventargs e)
{
label1.text=request.querystring["Name"];
label2.text=request.querystring["Email"];
}
Run, you can see the results after the delivery.

Second, the use of Session variables

The most common way to use the session variable is to pass the value to the next page and cross over to multiple pages until the value of the session variable is removed, and the variable disappears. For example, take a look at:
1. Create a Web Form
2. Place a button1 in the new Web form, placing two Textbox1,textbox2
3. Create Click events for button buttons
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, a new target page named WebForm2
5. Place two Label1,label2 in WebForm2
Add the following code to the 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, you can see the results after the delivery.

Iii. Use of Server.Transfer

Although this approach is a bit complicated, it's also a way to pass a value on a page.
For example, take a look at:
1. Create a Web Form
2. Place a button1 in the new Web form, placing two Textbox1,textbox2
3. Create Click events for button buttons
The code is as follows:
private void (object Sender,system.eventargs e)
{
Server.Transfer ("webform2.aspx");
}
4, the creation process to return the value of the Textbox1,textbox2 control code is as follows:
public string Name
{
get {return textbox1.text;}
}
public string Email
{
Get{return TextBox2.Text;}
}
5, a new target page named WebForm2
6. Place two Label1,label2 in WebForm2
Add the following code to the Page_Load of WebForm2:
private void Page_Load (Object Sender,system.eventargs e)
{
To create an instance of WebForm
WebForm1 WF1;
Get the instantiated handle
wf1= (WebForm1) Context.Handler;
Label1.text=wf1. Name;
Label2.text=wf1. Email;
}

These three methods are common.



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.