.NET how to implement parameters between the page

xiaoxiao2021-03-06  158

Using queryString Using QuerySting The transfer 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 displayed on the address bar of the browser. (Insecure), it is not possible to pass objects, but this method is still a good solution if the value is small and the safety requirements are not high. 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"];

Using the Session Variable Use the session variable to pass the value between the page, in this case, in this example, the value in the control is in the session variable, and then use it in another page, realize the value between different pages. The purpose of passing. 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 // controlsSession [ "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 ");} Using Server.Transfer This method is slightly complicated than the method described above, but in the page value transfer It is especially useful. Use this method You can access the revealed values ​​in another page, of course, using this method, you need to write some code to create some properties so you can be in another The page is accessible, but the benefits of this way are 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 formWebForm1 wf1; // get reference to current handler instancewf1 = (WebForm1) Context.Handler; Label1.Text = wf1.Name; Label2.Text = wf1 .Email;} Enable a cross-page transfer function in ASP.NET 2.0, its features and usage are in the future!

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

New Post(0)