C # page transfer value

Source: Internet
Author: User

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 ("name ");
Cookie_name.value = label1.text;
Reponse. appendcookie (cookie_name );
Server. Transfer ("B. aspx ");
}

B. C # code in aspx
Private void page_load (Object sender, eventargs E)
{
String name;
Name = request. Cookie ["name"]. value. tostring ();
}

5. Use the server. Transfer Method
This can be said to be the method used for face object development. It uses server. the transfer method directs the process from the current page to another page. The new page uses the response stream of the previous page. Therefore, this method is completely object-like and concise and effective.
A. aspx C # code
Public string name
{
Get {return label1.text ;}
}
Private void button#click (Object sender, system. eventargs E)
{
Server. Transfer ("B. aspx ");
}

B. C # code in aspx
Private void page_load (Object sender, eventargs E)
{
A newweb; // instance a form
Newweb = (source) Context. Handler;
String name;
Name = newweb. Name;
}

ASP. NET web forms provides developers with an excellent event-driven development mode. However, this simple application development mode brings us some small problems. For example, in the traditional ASP application, you can use the POST method to easily transfer one or more values from one page to another (Request ()/request. form ()/request. querystring (), using the same method in ASP. net. Here, we can solve this problem in other ways. ASP. NET provides three methods for us. One is to use querystring to transmit the corresponding value, the other is to transmit the corresponding value through the session variable, and the other is to use the server. the transfer method.

I. querystring
Querystring is a simple value transfer method. Its disadvantage is that it displays the value to be transferred in the address bar of the browser and cannot pass objects in this method. If you want to pass a security that is not so important or a simple value, it is best to use this method. The following is a small example to complete the data transfer process. The procedure is as follows:
1. Create a web form
2. Place a button1 in the new web form, and place two textbox1 and textbox2
3. Create a click event for the button
The Code is as follows:
Private void button_click (Object sender, system. eventargs E)
{
String URL;
Url = "webform2.aspx? Name = "+ textbox1.text +" & Email = "+ textbox2.text;
Response. Redirect (URL );
}
4. Create a new target page named webform2
5. Place label1 and label2 in webform2.
Add the following code to page_load of webform2:
Private void page_load (Object sender, system. eventargs E)
{
Label1.text = request. querystring ["name"];
Label2.text = request. querystring ["email"];
}
Run the command to view the passed result.

Ii. Use session Variables
Using session variables to pass values is the most common method. In this method, values can be transmitted not only to the next page, but also to multiple pages, the variable does not disappear until the value of the session variable is removed. For example:
1. Create a web form
2. Place a button1 in the new web form, and place two textbox1 and textbox2
3. Create a click event for the button
The Code is as follows:
Private void button_click (Object sender, system. eventargs E)
{
Session ["name"] = textbox1.text;
Session ["email"] = textbox2.text;
Response. Redirect ("webform2.aspx ");
}
4. Create a new target page named webform2
5. Place label1 and label2 in webform2.
Add the following code to page_load of webform2:
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 ");
}
Run the command to view the passed result.

Iii. Use server. Transfer
Although this method is a bit complicated, it is also a way to pass values on the page.
For example:
1. Create a web form
2. Place a button1 in the new web form, and place two textbox1 and textbox2
3. Create a click event for the button
The Code is as follows:
Private void (Object sender, system. eventargs E)
{
Server. Transfer ("webform2.aspx ");
}
4. The textbox1 is returned during the creation process. The value code of the textbox2 control is as follows:
Public string name
{
Get {return textbox1.text ;}
}
Public String email
{
Get {return textbox2.text ;}
}
5. Create a new target page named webform2
6. Place label1 and label2 in webform2.
Add the following code to page_load of webform2:
Private void page_load (Object sender, system. eventargs E)
{
// Create a webform instance
Webform1 WF1;
// Obtain the instantiated handle
WF1 = (webform1) Context. Handler;
Label1.text = wf1.name;
Label2.text = wf1.email;

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.