Several methods of passing the values ​​between the ASP.NET page

xiaoxiao2021-03-06  63

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: First, using queryString queryString is a very simple transmission method, the shortcoming is that the value to be transmitted is displayed in the browser's address bar, and the object is not possible 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 form 2, placed 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) {string url; url = "webform2.aspx name =?" TextBox1.Text "& email =" TextBox2.Text; Response.Redirect (url);} 4, a new target page named webform2 5, disposed in two WebForm2 the Label1, Label2 WebForm2 add the following code to the Page_Load: 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, using the session variable using the session variable transmission 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, Variables will disappear.

To take a look: 1. Create a web form 2, placed a button1 in the new web form, set two TextBox1, TextBox2 3, create a 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, newly built a target page named Webform2 5, Place two label1 in WebForm2, Label2 adds 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");} Run, 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 form 2, placed a button1 in the new web form, set two TextBox1, TextBox2 3, create a click event code for the Button button as follows: 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, placed two Label1 in WebForm2, Label2 Add the following code in WebForm2: 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;}, you can see it The result was passed. Author Blog:

Http://blog.9cbs.net/ekin7913046/

转载请注明原文地址:https://www.9cbs.com/read-114465.html

New Post(0)