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, using queryStringQueryString is a very simple transmission method, the disadvantage is that the value to be transmitted is displayed in the address bar of the browser, and the object 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 a small example, the steps are completed, the steps are as follows: 1. Create a web form2, placed a button1 in the newly built web form, set two TextBox1, TextBox2 3, create the Click event code for the Button button: Private void button1_click (object sender, system.eventargs e) {string url; url = "? webform2.aspx name =" textbox1.text "& email =" textbox2.text; response.redirect (url);} 4, new a target page named webform25, placed in two label1 webform2, label2 add the following code WebForm2 page_load of the: 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. To take a look: 1. Create a web form2, place a Button1 in the newly built web form, set two TextBox1, TextBox2 3, create the Click event code for the Button button as follows: Private void button1_click (Object Sender, System. Eventargs e) {session ["name"] = textbox1.text; session ["email"] = textBox2.text; response.redirect ("Webform2.aspx");} 4, New Target page named Webform25, in Webform2 Place two Label1, Label2 Add the following code in the 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");} runs, you can see the resulting result. Third, using Server.Transfer although this method is a bit complicated, it is not a way to pass on the page. To take a look: 1. Create a web form2, place a Button1 in the newly built web form, set two TextBox1, TextBox2 3, create the Click event code for the Button button as follows: Private void button1_click (Object Sender, System. Eventargs e) {Server.Transfer ("WebM2.aspx");} 4, create a process to return to TextBox1, 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, a new target page named webform26, placed in two label1 in webform2, label2 add the following code webform2 page_load of the: private void page_load (object sender, system. Eventargs e) {// Create an instance of the original form WebForm1 WF1; // Get instantiated handle WF1 = (WebForm1) context.handler; label1.text = wf1.name; label2.text = wf1.email;
} Operation, you can see the resulting result. -
http://blog.9cbs.neet/hbzxf