page Transfer value

Source: Internet
Author: User
Tags variable scope

in the cooperative development, in the page string value of time, encountered some difficulties, in the online to find a bit, found a lot of ways to pass the value can be easily divided into the following three kinds.

First,URLPass Value

The value of the original page is placed on the target page. URL and then through the QueryString method to obtain the value. However, its disadvantage is that the value passed is displayed in the browser's address bar (unsafe), but also cannot pass the object, but in the case of low delivery of values and security requirements are not high, this method is a good solution. The following shows the usage:

Source page WebForm1.aspx.cs part of the code in:

private void Button1_Click (object sender, System.EventArgs e) {string URL;     Url= "Webform2.aspx?name=" + TextBox1.Text + "&email=" + TextBox2.Text; Response.Redirect (URL);}

Part of the code in the target page WebForm2.aspx.cs :

private void Page_Load (object sender, System.EventArgs e) {label1.text=request.querystring["name"]; label2.text=request.querystring["email"];}

Second, the object property value

this kind of method is slightly more complicated than the method described above, but it is particularly useful in the transfer of values between pages, and using this method you can access the exposed values in the same way as object properties, and of course, using this method, You need to write some extra code to create some properties so that you can access it on another page, but the benefits of this approach are also obvious. In general, using this approach is concise and object-oriented.

1. using Server.Transfer

The implementation steps are as follows:

1. Add the necessary controls to the page

2, create a Get property procedure that returns a value

3. Create a button and link button that returns the form

4, call the Server.Transfer method in the button click event handler to transfer to the specified page

5, on the second page, we can use the Context.Handler property to get a reference to the previous page instance object, through which you can use the value of the control that accesses the previous page

Part of the code in the source page WebForm1.aspx.cs:

public string name{get {return textbox1.text;}} public string email{get {return textbox2.text;}}

Then call the Server.Transfer method

private void Button1_Click (object sender, System.EventArgs e) {Server.Transfer ("webform2.aspx");}

target page code:

In webform2.aspx, be sure to add < Reference page= "~/webform1.aspx" > or < PreviousPageType virtualpath= "in the first sentence ~/     WebForm1.aspx ">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;}

2. Using PreviousPageType

This command is a new directive in. net2.0, which deals with the new cross-page delivery functionality that is provided by ASP. Specifies the page from which the routing process for a fixed span page starts. Contains two properties:

TypeName: Sets the derived class name when loopback is set

VirtualPath: Sets the address of the page that is transmitted when it is echoed.

The following example:

The source page WebForm1.aspx has a textbox,id of txtname. Set a property in WebForm1.aspx.cs:

Public TextBox name{Get{return This.txtname;} Returns a Control object}
In the design file of the target page

Above plus < PreviousPageType virtualpath= "~/page1.aspx" >,//then you can refer to the properties defined in WebForm1.aspx. lblname.text= "Hello" + "";

In the above two methods, one of the words we can clearlypreviouspage"In fact, this involves a page of the original and the target to jump to. Next, take a look at the addition can satisfy the jump relationship between the page, the non-jump between the page can also pass the value of the method:

Third, the public variable transfer value

This kind of method makes the page pass the value to be more convenient, the commonly used value, generally is uses the broad scope the variable, may include in the public variable scope:

1. Use the session variable

using session variables is another way to pass values between pages, in this case we have the values in the control in the Session variable, It is then used in another page to achieve the purpose of passing values between different pages. However, it is important to note that storing too much data in the session variable consumes more server resources and should be used with caution when using the session , of course, We should also use some cleanup actions to remove unwanted sessions to reduce the unnecessary consumption of resources.

Part of the code in the value- WebForm1.aspx.cs page:

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 ("webform2.aspx");}

value The part of the code in the page WebForm2.aspx.cs:

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");}
2. using Cookie object variables

This is also common use of the method, as in the session, for each user, but there is an essential difference, that is, the cookie is stored in the client, and the session is stored on the server side. And the use of cookies should be used in conjunction with the ASP.

C # code for a.aspx

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

C # code in b.aspx

Private Voidpage_load (object sender, EventArgs e) {    string name;    Name = request.cookie["Name"]. Value.tostring ();}

3.UseApplicationObject Variables

The Application object is scoped to the whole global, which means it is valid for all users. Its common method is to use lock and unlock.

C # code for a.aspx

private void Button1_Click (object sender, System.EventArgs e) {application["name"] = Label1.Text; Server.Transfer ("b.aspx");}

C # code in b.aspx

private void Page_Load (object sender, EventArgs e) {string name;    Application.Lock (); Name = application["Name"].    ToString (); Application.UnLock ();}

On the surface, there are many ways to implement the value of the page, but from the realization of the fundamental view, but also the three kinds of! page Transfer value

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.