ASP.NET five kinds of global variables! Pass values between pages method

Reprinted from the product is slightly Library: http://www.pinlue.com/article/2020/03/0904/5810000440837.html

 

1. Use QueryString QueryString variable is a very simple way by value, he can transfer the value displayed in the address bar of your browser. If the transmission is a simple value or more or less demanding safety structure, this method may be used. But for passing an array or object, it can not be used in this method has. Here is an example: a.aspx C # code private void Button1_Click (object sender, System.EventArgs e) {string s_url; s_url = "? B.aspx name =" + Label1.Text; Response.Redirect (s_url);} b.aspx the C # code private void Page_Load (object sender, EventArgs e) {Label2.Text = Request.QueryString [ "name"];}

2. Using the Application object variable Application object is the entire global scope, that are valid for all users. Lock with their usual methods and UnLock. a.aspx C # code private void Button1_Click (object sender, System.EventArgs e) {Application [ "name"] = Label1.Text; Server.Transfer ( "b.aspx");} b.aspx the C # code private void Page_Load (object sender, EventArgs e) {string name; Application.Lock ();. name = Application [ "name"] ToString (); Application.UnLock ();}

3. Use Session variables must we use this is certainly the most common usage, and its operation is similar to the Application, acting on the individual user, so that excess storage can lead to depletion of server memory resources. a.aspx C # code private void Button1_Click (object sender, System.EventArgs e) {Session [ "name"] = Label.Text;} b.aspx the C # code private void Page_Load (object sender, EventArgs e) {string name ;. name = Session [ "name"] ToString ();}

4. Use Cookie object variable that is often used by everyone, and Session, as it is even for every user, but there is an essential difference, that Cookie is stored on the client, and the session is stored on the server end. And to tie in ASP.NET Cookie use built-in objects Request to use. a.aspx C # code private void Button1_Click (object sender, System.EventArgs e) {HttpCookie cookie_name = new HttpCookie ( "name"); cookie_name.Value = Label1.Text; Reponse.AppendCookie (cookie_name); Server.Transfer ( " b.aspx ");} b.aspx the C # code private void Page_Load (object sender, EventArgs e) {string name; name = Request.Cookie. [" name "] value.ToString ();}

5. Server.Transfer method can be said that this method is used the development of image object surface, using the method Server.Transfer flow directed from the current page to another page, a page response using the stream before the new page, Therefore, this method is completely the surface of the image object, simple and effective. a.aspx C # code public string Name {get {return Label1.Text;}} private void Button1_Click (object sender, System.EventArgs e) {Server.Transfer ( "b.aspx");} b.aspx the C # code private void Page_Load (object sender, EventArgs e) {a newWeb; // instance a form newWeb = (source) Context.Handler; string name; name = newWeb.Name;}

ASP.NET provides excellent event-driven programming model that allows developers to simplify the overall design of the application, but this also caused some of the problems inherent in it, for example, using classic ASP, we can easily by using the POST method pass values ​​between pages to achieve the same thing, using event-driven programming model of ASP.NET is not so easy, of course, we still have some ways to achieve the same function. This article will try to use different possible ways to solve this problem, it is anticipated that this article will include the use querystring, session variables, and server.Transfer ways to achieve value passed between pages.

Use QueryString

Use QuerySting pass values ​​between pages is already a very old mechanism, the main advantage of this method is very simple to implement, but its drawback is that the value of the transfer is displayed on the browser's address bar (not safety), while not passing objects, but in less value and less demanding security situation passed, this method is a good program. Using this method the following steps:

1, use the controls to create a web form (form)

2, you can return the form to create buttons and link buttons

3. Create a character variable in the URL saved button or link button click event in

4, add QueryString parameter in the URL saved

5, use Response.Redirect to redirect the saved URL above

The following code snippet demonstrates 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);

}

Goal page code:

private void Page_Load

(object sender, System.EventArgs e)

{

Label1.Text=Request.QueryString["name"];

Label2.Text=Request.QueryString["email"];

}

Use Session Variables

Session variables can be used in another way between the values ​​of the pages transmitted, in this example we present Session variable value in the control, and then use it in another page in order to achieve different values ​​passed page object . However, it should be noted that storing too much data consumes more server resources, should be careful when using session in Session variables, of course, we should also use some clean-up operation to remove some unwanted session to reduce the resources unnecessary consumption. Session variables passed using general procedure the following values:

1, add the necessary controls in the page

2, you can return the form to create buttons and link buttons

3, in a button or link button click event, the added value of the control variable in the session

4, using the method to redirect to another page Response.Redirect

5, extract the value of another page in the session, when the session is determined without the use, it should be explicitly clear

The following code snippet demonstrates how to implement this method:

Source page code:

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");

}

Goal 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");

}

Use Server.Transfer

The methods described above compared to a little more complicated, but it is particularly useful in the value passed between the page, you can use this method in a way to access the object attribute value revealed in another page, of course, the use of in this way, you need to write additional code to create some properties so that you can access it in another page, however, the benefits of this approach are obvious. Overall, the use of this method is simple but it is also an object-oriented. Using this method the overall process is as follows:

1, add the necessary controls in the page

2. Create a Property Get procedure return value

3, you can return the form to create buttons and link buttons

4, call the Server.Transfer method transfers to a specific page in a button click event handler

5, in the second page, we can use Context.Handler property to obtain the previous page reference instance of an object, through which, on the value of a page before accessing control may be used

Comprehensive following code is code that implements 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");

}

Goal page code:

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;

}

Published 60 original articles · won praise 52 · views 110 000 +

Guess you like

Origin blog.csdn.net/yihuliunian/article/details/104754664