Cooperative Development-data transfer between ASP. Net pages and Cooperative Development -asp.net

Source: Internet
Author: User

Cooperative Development-data transfer between ASP. Net pages and Cooperative Development -asp.net

During the cooperative development, some difficulties were encountered when uploading values on the page. I searched the internet and found that there are many value passing methods, which can be divided into the following three simple methods.

1. URL-based value transfer

The value of the original page is placed in the URL of the target page, and then obtained through the QueryString method. However, its disadvantage is that the passed value is displayed on the address bar of the browser (Insecure), and the object cannot be passed, however, this method is a good solution when the number of transmitted values is small and the security requirements are not high. The usage is as follows:

Some code in the Source Page WebForm1.aspx. cs:

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

Some code in WebForm2.aspx. cs on the target page:

private void Page_Load(object sender, System.EventArgs e){     Label1.Text=Request.QueryString["name"];     Label2.Text=Request.QueryString["email"];}

Ii. Pass Object Property Values

This type of method is a little more complex than the method described above, but it is particularly useful in inter-page value transfer, using this method, you can access the exposed value in the form of Object Attributes on another page. Of course, this method is used, you need to write additional code to create some attributes so that you can access it on another page. However, the benefits of this method are also obvious. In general, this method is concise and object-oriented.

1. Use Server. Transfer

The implementation steps are as follows:

1. Add necessary controls to the page.

2. Create the Get attribute process of the returned value

3. Create buttons and link buttons that can return the form

4. click the button to Transfer the Server. Transfer Method to the specified page.

5. On the second page, we can use Context. the Handler property is used to obtain the reference of the previous page instance object. With it, you can access the value of the control on the previous page.

Some 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, add <Reference Page = "~ /WebForm1.aspx "> or <PreviousPageType VirtualPath = "~ /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. Use PreviousPageType

This command is a new command in. net2.0. It is used to process the new cross-page transfer function provided by ASP. NET 2.0. It is used to specify the page on which the cross-page transfer process starts. It contains two attributes:

TypeName: Specifies the name of the derived class during the delivery.

VirtualPath: Set the address of the page to be sent during delivery.

Example:

The Source Page WebForm1.aspx contains a TextBox with the ID txtName. Set an attribute in WebForm1.aspx. cs:

Public TextBox Name {get {return this.txt Name;} // return a control object}
In the design file of the target page

// Add <PreviousPageType VirtualPath = "~ /Page1.aspx ">, // then you can reference the attributes defined in WebForm1.aspx. lblName. Text =" Hello "+ PreviousPage. Name. Text + "";

In the above two methods, we can clearly define the word "PreviousPage", which involves an original page and the target page to jump. Next, let's take a look at the method that can pass values between pages in addition to the jump relationship. You can also pass values between non-jump pages:

3. Pass public variables

This type of method makes it easier to pass page values. Common Values, usually variables with a wide range of use, can be included in the scope of public variables:

1. Use Session Variables

Using the Session variable is another way to pass values between pages. In this example, we store the value in the control in the Session variable and then use it on another page, to transfer values between different pages. However, it should be noted that storing too much data in the Session variable will consume a lot of server resources, so you should be careful when using the session. Of course, we should also use some cleanup actions to remove unnecessary sessions to reduce unnecessary resource consumption.

Some codes in WebForm1.aspx. cs on the value passing 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");}

Some codes in the value 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. Use Cookie object variables

This is also a common method. Like Session, it is for every user, but there is a fundamental difference, that is, cookies are stored on the client, session is stored on the server. In addition, the use of cookies should be used in combination with the ASP. NET built-in Object Request.

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. C # code in aspx

private voidPage_Load(object sender, EventArgs e){    string name;    name = Request.Cookie["name"].Value.ToString();}

3. Use the Application object variable

The scope of the Application object is global, that is, it is valid for all users. The common methods are Lock and UnLock.

A. aspx C # code

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

B. C # code in 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 implementation methods for transferring values between pages, but basically, there are only three methods!
Several methods for transferring values between ASPNET pages

1. Use QueryString variable
QueryString is a simple method for transferring values. It can display the transmitted values in the address bar of a browser. This method can be used to transmit one or more numeric values with low security requirements or simple structure. However, this method cannot be used to pass arrays or objects. The following is an example:
A. aspx C # code
Private void button#click (object sender, System. EventArgs e)
{
String s_url;
S_url = "B. aspx? Name = "+ Label1.Text;
Response. Redirect (s_url );
}

B. C # code in aspx
Private void Page_Load (object sender, EventArgs e)
{
Label2.Text = Request. QueryString ["name"];
}

2. Use the Application object variable
The scope of the Application object is global, that is, it is valid for all users. The common methods are Lock and UnLock.
A. aspx C # code
Private void button#click (object sender, System. EventArgs e)
{
Application ["name"] = Label1.Text;
Server. Transfer ("B. aspx ");
}

B. C # code in aspx
Private void Page_Load (object sender, EventArgs e)
{
String name;
Application. Lock ();
Name = Application ["name"]. ToString ();
Application. UnLock ();
}

3. Use Session Variables
Presumably, this is definitely the most common usage. Its operations are similar to those of the Application, which act on individual users. Therefore, excessive storage will result in the depletion of server memory resources.
A. aspx C # code
Private void button#click (object sender, System. EventArgs e)
{
Session ["name"] = Label. Text;
}

B. C # code in aspx
Private void Page_Load (object sender, EventArgs e)
{
String name;
Name = Session ["name"]. ToString ();
}

4. Use Cookie object variables
This is also a common method. Like Session, it is for every user, but there is a fundamental difference, that is, cookies are stored on the client, session is stored on the server. In addition, the use of cookies should be used in combination with the ASP. NET built-in Object Request.

A. aspx C # code
Private void button#click (object sender, System. EventArgs e)
{
HttpCookie cookie_name = new HttpCookie (& qu ...... remaining full text>

Several methods for transferring values between ASPNET pages

However, in general, QueryString, Session, Cookies, Application, Server. Transfer are commonly used. I. QueryString is a very simple method for transferring values. It can display the transmitted values in the browser's address bar. This method can be used to transmit one or more numeric values with low security requirements or simple structure. However, this method cannot be used to pass arrays or objects. Advantages of this method: 1. Simple to use, it is very effective to transmit numbers or text values when security requirements are not high. Disadvantages of this method: 1. Lack of security because its value is exposed in the URL address of the browser. 2. Objects cannot be passed. Method of use: 1. Use the name and value to be passed in the Code on the Source Page to construct the URL address. 2. Use Response. Redirect (URL); to Redirect the code on the source page to the above URL. 3. Use Request. QueryString ["name"]; In the code on the target page to retrieve the value passed in the URL address. Example: (1) a. aspxprivatevoid button#click (object sender, System. EventArgs e) {string s_url; s_url = "B. aspx? Name = "+ Label1.Text; Response. redirect (s_url);} (2) B. aspxprivatevoid Page_Load (object sender, EventArgs e) {Label2.Text = Request. queryString ["name"];} II. The Session must be the most common usage. Its operations are similar to those of the Application, and are applied to the user, excessive storage will consume the server's memory resources. Advantages: 1. Simple to use, not only can pass simple data types, but also can pass objects. 2. The data size is unlimited. Disadvantages: 1. storing a large amount of data in the Session variable will consume a lot of server resources. 2. easy to lose. Usage: 1. create the Name and Value you need to pass in the Code on the Source Page to construct the Session variable: Session ["Name"] = "Value (Or Object)"; 2. the code on the target page uses the Session variable to retrieve the passed value. Result = Session ["Nmae"] Note: You can destroy a session when it is not in use. The destroy method is to clear a Session. remove ("session name"); clear all: sessions. clear (); example: (1). aspxprivatevoid button#click (object sender, System. eventArgs e) {Session ["name"] = Label. text;} (2) B. aspxprivatevoid Page_Load (object sender, EventArgs e) {string name; name = Session ["name"]. toString () ;}3. Cookie is also a common method. Cookies are used to store small pieces of information in the user's browser and store user-related information, for example, when a user accesses a website, the user ID and user preferences The user can retrieve the previous information from the next visit. Therefore, cookies can also pass values between pages. Cookie uses the HTTP Header between the browser and the server... the remaining full text>

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: info-contact@alibabacloud.com 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.