10 classic methods for passing parameters in Asp.net

Source: Internet
Author: User

Client
1. query string requrystring --- use the URL to exchange data between the client and server
2. Hidden form fields-set and read data in form fields
3. Cookie --- data stored on the customer's browser
4. View status viewstate --- save page-related data
· Server
5. Application application-this data can be used by all users throughout the application lifecycle.
6. Session session --- this data is in contact with every user
7. Saved status context --- this data exists in a single request
8. cache --- this data is similar to the application
9. Other physical data storage media, such as databases, TXT texts, and XML files
10. Use the server. Transfer Method


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. aspxname =" + label1.text;
Response. Redirect (s_url );
}

B. C # code in aspx
Private void page_load (Object sender, eventargs E)
{
Label2.text = request. querystring ["name"];
}
Note:
If the parameter is a Chinese character, garbled characters may occur or the last Chinese character may be lost.
The solution is as follows:
Method 1. server. Net can be used in Asp.net. urlencode (parameter) encrypts the parameter, passes it in querystring, accepts the parameter in the receiver, and then uses server. urldecode (parameter) decrypts the parameter and then OK. In JavaScript, encodeurl and decodeurl can be used for encryption and decryption.
Method 2. Add a string after the Chinese parameter and remove the character after the receiver accepts it. Although it is a bit tricky, it has been tried and tested!

2. Hidden form fields
It is also feasible to transmit data between the client and server using hidden form fields.

For example:
<Input type = "hidden" name = "AA" value = "ABC">

String STR = request. Form ["AA"];
STR is equal to "ABC"

In summary, it is recommended that you do not store high-security data, such as passwords or bank card numbers.

3. 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 ();
}

4. View status viewstate --- save page-related data

Enable the viewstate attribute in the control.

5. 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 ();
}

6. 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 ();
}

7. Saved status context --- this data exists in a single request

ASP. NET provides a class system. Web. httpcontext to indicate the context. This object has an attribute items
The temporary storage Status uses the httpcontext. Items attribute to store data.
In msdn, The httpcontext. Items attribute is interpreted as follows: Get the key value that can be used to organize and share data between ihttpmodule and ihttphandler during HTTP requests
The httpcontext. Items attribute can store any type of data. No matter what data is stored in this attribute, it will be automatically cleared after the request processing is complete. This is the temporary storage status, and the data storage time is very short.

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

10. 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;
}

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.