This article describes how to transfer values between ASP. NET forms. For more information, see.
Assume that the ParentForm. aspx page contains TextBox1 text box and Open button.
Click the Open button to bring up SubForm. aspx. The SubForm. aspx page contains the TextBox1 text box and the Close button.
Click Close to Close the SubForm. aspx page, and display the value of the SubForm. aspx text box on the subpage to the ParentForm. aspx text box on the parent page.
Front-end code of the parent form:
The Code is as follows: |
Copy code |
<Script type = "text/javascript"> Function OpenSubForm (ret ){ Var strPath = "subForm. aspx" Var nHeight = 500 Var nWidth = 500 Var feature Feature = "Height =" + nHeight + ", Width =" + nWidth + ", top = 30, Left = 30 "; Feature + = ", dependent = yes, location = no, resizable = yes, scrollbars = yes, status = yes, toolbar = no ;"; Window. open (strPath + "? Ret_Form = Form1 & Ret_Value = "+ ret, 'subform', feature). focus (); Return false; } </Script> |
Background code of the parent form:
The Code is as follows: |
Copy code |
Private void Page_Load (object sender, System. EventArgs e) { // Response? What is the initial phase of zookeeper? Why? Why? Why are there too many other users? Q? Br/> this. Button1.Attributes. Add ("onClick", "return OpenSubForm ('textbox1 ');"); } |
Subform background code:
The Code is as follows: |
Copy code |
Private void button#click (object sender, System. EventArgs e) { String strScript = string. Empty; String strRetForm = String. Empty; String strRetValue = String. Empty; StrRetForm = Request. Params ["Ret_Form"]; StrRetValue = Request. Params ["Ret_Value"]; If (strRetForm = string. Empty) { StrRetForm = "document. forms [0]"; } StrScript = "<script language = javascript> "; StrScript + = "window. opener." + strRetForm; StrScript + = "." + strRetValue + ". value = '" + this. TextBox1.Text. Trim () + "';"; StrScript + = "window. close ();"; StrScript + = "</script> "; Response. Write (strScript ); }
|
The above js is actually page value transfer. Below I will send some code for page value transfer for your reference.
Several methods for transferring values between pages.
The following code snippet demonstrates how to implement this method:
Some code in the Source Page WebForm1.aspx. cs:
The Code is as follows: |
Copy code |
Private void button#click (object sender, System. EventArgs e) { String url; Url = "WebForm2.aspx? Name = "+ TextBox1.Text +" & email = "+ TextBox2.Text; Response. Redirect (url ); } Some code in WebForm2.aspx. cs on the target page: Private void Page_Load (object sender, System. EventArgs e) { Label1.Text = Request. QueryString ["name"]; Label2.Text = Request. QueryString ["email"]; } |
Use Session Variables
Some code in the Source Page WebForm1.aspx. cs:
The Code is as follows: |
Copy code |
Private void button#click (object sender, System. EventArgs e) { // Textbox1 and textbox2 are webform // Controls Session ["name"] = TextBox1.Text; Session ["email"] = TextBox2.Text; Server. Transfer ("WebForm2.aspx "); }
|
Some code in WebForm2.aspx. cs on the target page:
The Code is as follows: |
Copy code |
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 "); } |
The above two types are commonly used and are not described. You can refer to them on your own.