Several methods for transferring values between pages in asp.net

Source: Internet
Author: User

1. Get (explicitly passed using QueryString)
Method: Follow the url with parameters.
Features: simple and convenient.
Disadvantage: the string can contain a maximum of 255 characters, and the data is leaked in the url.
Applicable data: simple, small, and key data.
Applicability: pass data to the user and to another target page. It is often used to transfer data between two pages.
Usage: for example, what is the url suffix? UserID = ..., Go to the target page. The target page can use Request. QueryString ["InputText"] On the servo side to obtain the specified parameter value.
Http://www.itokit.com
2. Post
Method: common method. Use form to submit.
Features: The most common method. A common technique is to submit hidden data in a hidden domain by form.
Applicable data: massive data, including file upload.
Applicability: Same as the Get Method
Usage: submit after the action target is specified in the form client, use server. Transfer (url) for submission in the asp.net servo end, and obtain it using Request. Form ["FormFieldID"] in the servo end.

3. Page Object Attributes
Method: exclusive to asp.net. Use HttpContext to obtain information about the requested page.
Features: objects can be stored directly.
Disadvantage: note that the forced conversion type must be correct.
Applicable data: various data.
Applicability: transfer complex data between pages.
Usage: Get Context. Handler to convert it to an instance of the page sending the request, and then you can easily access fields and even attributes in the form. The Context refers to the HttpContext object, Handler is its attribute, and Context. handler can be understood as an instance variable for creating the Source Page class. After obtaining this instance, you can directly access the attributes and Public methods.

4. cookie
Method: Classic method for storing data on the client.
Disadvantages: low security, limited by client settings, a site only has 20 cookies, each with a capacity of 4096 bytes.
Validity Period: custom or cleared by users
Applicable data: user session data (generally user names, user personalized settings, etc)
Applicability: single user, All pages of the entire site
Usage: Reference Request. Cookies (read information), Response. Cookies (write information), or use HttpCookieCollection to directly create Cookie objects.

5. Session
Method: store user data on the servo end.
Features: In asp.net, you can set the session storage mode, location, and whether the session ID is stored Based on cookies.
Objects can be stored directly.
Disadvantages: potential failure in asp.net
Validity Period: user activity time + custom delay.
Applicable data: user-specific information.
Applicability: single user, All pages of the entire site.
Usage: Session ["CollectionName"] = value/object;

6. Cache
Method: store user data in the servo data cache.
Features: greatly improves efficiency. Objects can be stored directly.
Disadvantage: the data is not updated in a timely manner.
Validity Period: Application lifecycle or custom.
Applicable data: data that can be shared by all pages and users.
Usage: Cache ["CollectionName"] = value | object;

7. Appliction
Method: store the data here, which is equivalent to a global variable.
Features: objects can be stored directly. Shared data of the entire site.
Validity Period: Application lifecycle.
Applicable data: data shared by all pages and users.
Usage: Appliction ["CollectionName"] = value | object;

8. ViewState
Method: asp.net special mechanism, used to restore the page status.
Feature: the controls on the page and their stored data are serialized in a hidden domain named _ ViewState.
Disadvantage: it is in HTML and has low security. Encryption and verification can be set, but the data volume increases greatly and the efficiency may be affected.
Applicable data: the data to be stored in the page PostBack. Too much data will affect the page sending efficiency.
Applicability: save data on the page.
Usage: ViewState ["CollectionName"] = value;

9. Static
Method: store data in static variables.
Features: improves efficiency.
Disadvantages: poor use may cause data disorder between users or pages, causing great risks. We recommend that you assign a value only once. You cannot change this value for a single user.
Applicable data: data shared by all users.
Applicable scope: all such page instances.
Usage: declare static variables in the class.
Organize:
The ViewState and static variables are available for saving data on the page.
Get, post, and HttpContext are commonly used for data transmission between pages. Of course, you can use them to save your own data.
Common session and cookie sharing for all page data of a single user can also be applied in the above two cases.
Commonly used Appliction, cache, and static variables for data sharing of the entire application (all user pages.

Related Article

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.