Transfer value between two ASP.NET pages

xiaoxiao2021-03-06  63

1. Use queryString to use querysting to pass the value between the page is already a very old mechanism. The main advantage of this method is very simple, but its disadvantage is that the value passed is to display the address bar of the browser. The upper (unsafe), but also cannot transmit the object, but in the case where the value is less than the safety requirements, this method is still a good solution. The steps to use this method are as follows: 1. Use the control to create a Web Form (Form) 2. Create a button and link button 3 that can return a form. Create a character variable 4 that saves the URL in the click event of the button or link button. Add queryString parameter 5 in the saved URL 5, use Response.Redirect to redirect the code snippet below to demonstrate how to implement this method: Source page code: Private void button1_click (Object sender, system.eventargs e) {String url; url = "? anotherwebform.aspx name =" TextBox1.Text "& email =" TextBox2.Text; Response.Redirect (url);} target page code: private void Page_Load (object sender, System.EventArgs e) {Label1.text = Request.QueryString ["name"]; label2.text = request.queryString ["email"];

2, using the session variable using the session variable is another way to pass the value between the page, in this case, we exist in the session variable in this example, then use it in another page, inter-page Implement the purpose of value. However, it should be noted that there are more data in SESSION variables to consume more server resources. When using sessions, it should be cautious, of course, we should also use some cleaning action to remove some unwanted session to reduce resources. Necessary consumption. The general steps of using the session variable transmission value are as follows: 1. Add the necessary controls in the page, create buttons and link buttons that can return to the form, add the value of the control to the session on the button or link button, add the value of the control to the session Variables 4, use the response.redirect method to redirect to another page 5, extract the value of the session in another page, when you are determined that you don't need to use the session, to explicitly clear it below the code snippet demonstrate how to implement this method : page code source: private void Button1_Click (object sender, System.EventArgs e) {// textbox1 and textbox2 are webform // controls Session [ "name"] = TextBox1.Text; Session [ "email"] = TextBox2.Text; Server.Transfer ("ANOTHERWEBFORM.ASPX");} Target Page 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");} 3, using server.transfer compared to the method described above is slightly complicated, but between the page The value transfer is especially useful. Using this method You can access the revealed values ​​in another page in an object properties, of course, use this method, you need to write some code to create some properties so you can Access it on another page, but this way is also obvious. Overall, it is a simple to use this method. The entire process of using this method is as follows: 1, add the necessary controls 2 in the page, create the return value of the GET property process 3, create the buttons and link buttons that can return to the form, click the button to call Server in the event handler .Transfer method Transfer to the specified page 5, in the second page, we can use the context.handler property to get the reference to the previous page instance object, through it, you can use the value of the previous page control The following code integrates the code of the above steps: Source page code: Add the following code to the page public string name {get {return textbox1.text;}}

public string EMail {get {return TextBox2.Text;}} then call Server.Transfer method private void Button1_Click (object sender, System.EventArgs e) {Server.Transfer ( "anotherwebform.aspx");} code for the target page: private void Page_Load (object sender, System.EventArgs e) {// create instance of source web form WebForm1 wf1; // get reference to current handler instance wf1 = (WebForm1) Context.Handler; Label1.Text = wf1.Name; Label2.Text = WF1.EMAIL;} General comparison promotion High security

Send a page:

Application ["SUM"] = this.TextBox1.Text;

Server.Transfer ("Webform2.aspx");

Receive page:

This.TextBox1.text = (string) Application ["SUM"];

Application is essentially a collection of all files in the entire virtual directory. If you want to use a variable value throughout the application, the Application object will be the best choice.

5, other ways

Plus a line in the HTML code of the received page: <% @ reference page = "Webform1.aspx"%>

WebForm1 fp = (WebForm1) Context.handler;

this.TextBox1.text = fp.name; // name is the first page of public variables

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

New Post(0)