ASP.NET Web Forms provides developers with an excellent event-driven development mode. However, this simple application development model has brought us some small problems. For example, in traditional ASP applications, you can easily transfer a value or multiple values via the Post method. To another page, use the same method to achieve a bit troubles in ASP.NET. Here, we can solve this situation in other ways. ASP.NET provides us with three ways, one is that the corresponding value can be transmitted by queryString, one is to transmit the corresponding value by the session variable, and is achieved by the Server.Transfer method. The following introduces one by one,
First, use querystring
QueryString is a very simple transmission method, which is that the value to be transmitted is displayed in the address bar of the browser and cannot be passed in this method. If you want to deliver a security is not so important or a simple value, it is best to use this method. By following a small example to complete the value, the steps are as follows:
1. Create a web form
2. Place a Button1 in the newly built Web Form, placed two TextBox1, TextBox2
3. Create a Click event for the Button button
code show as below:
Private void button1_click
(Object Sender, System.EventArgs E)
{
String URL;
URL = "WebForm2.aspx? Name ="
TEXTBOX1.TEXT "& email ="
Textbox2.text;
Response.Redirect (URL);
}
4. Newly built a target page named Webform2
5. Place two label1, label2 in WebForm2
Add the following code in the weight_load of WebForm2:
PRIVATE VOID PAGE_LOAD
(Object Sender, System.EventArgs E)
{
Label1.text = Request.QueryString ["name"];
Label2.text = Request.QueryString ["email"];
}
Operation, you can see the resulting result.
Second, use session variables
Using the session variable value is the most common way, this method can not only pass the value to the next page, but also pass to multiple pages until the value of the session variable is removed, the variable will disappear. For example, take a look:
1. Create a web form
2. Place a Button1 in the newly built Web Form, placed two TextBox1, TextBox2
3. Create a Click event for the Button button
code show as below:
Private void button1_click
(Object Sender, System.EventArgs E)
{
Session ["Name"] = textbox1.text;
Session ["email"] = textbox2.text;
Response.Redirect ("Webform2.aspx");
}
4. Newly built a target page named Webform2
5. Place two label1, label2 in WebForm2
Add the following code in the weight_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");
}
Operation, you can see the resulting result.
Third, use Server.Transfer
Although this method is a bit complicated, it is also a way to pass the value on the page.
For example, take a look:
1. Create a web form
2. Place a Button1 in the newly built Web Form, placed two TextBox1, TextBox2
3. Create a Click event for the Button button
code show as below:
Private void button1_click
(Object Sender, System.EventArgs E)
{
Server.Transfer ("Webform2.aspx");
}
4. Create a process to return to TextBox1, the value code for the TextBox2 control is as follows:
Public String Name
{
get
{
Return TEXTBOX1.TEXT;
}
}
Public String email
{
get
{
Return TEXTBOX2.TEXT;
}
}
5, newly built a target page named Webform2
6. Place two label1 in WebForm2, label2
Add the following code in the weight_load of WebForm2:
PRIVATE VOID PAGE_LOAD
(Object Sender, System.EventArgs E)
{
// Create an instance of the original form
WebForm1 WF1;
// Get an instantiated handle
WF1 = (WebForm1) context.handler;
Label1.text = wf1.name;
Label2.text = wf1.email;
}
Operation, you can see the resulting result.