Transfer value between two ASP.NET pages

xiaoxiao2021-03-06  78

Introduction ASP.NET provides an excellent event-driven programming model that allows developers to simplify the overall design of the application, but this also causes some problems inherent, for example, in the traditional ASP, we can use the Post method very Easily implement the page transfer value, the same thing, asp.net using the event-driven programming model is not so easy, of course, we still have some ways to achieve the same function. This article will try to use different possible methods to solve this problem, but it is foreseeable that this article will contain the value passed using querystring, session variables, and server.transfer methods.

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 // 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");} Using Server.Transfer, this method is slightly complicated than the method described above, but passes on page interval It is especially useful, using this method you can access the revealed values ​​in another page, of course, use this method, you need to write some code to create some properties so that you can A page access it, however, 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 form WebForm1 wf1; // get reference to current handler instance wf1 = (WebForm1) Context.Handler; Label1.Text = wf1.Name; Label2.Text = WF1.EMAIL;} General comparison promotes SESSION security

If you need a variable that you need to pass, you can save the value with a HashTable or array and save it to the session.

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

New Post(0)