Parameter transfer between pages in ASP. NET

Source: Internet
Author: User
Tags html header

ASP. NETTo transfer parameters between pages.


Prepared by: CC Dad




L recently I have been working on the problem of system integration login between Weaver OA and the company's self-developed systems. In the study of parameter passing between Weaver pages, we need to understand the current method of parameter passing between pages.


It can be classified into the following situations on the Internet and is recorded. For future reference

I,Use QueryString:

Using QueryString to pass values between pages is a very common method, which is often used in ASP.

Advantages and disadvantages
1. Easy to use. It is very effective for passing numbers or text values when security requirements are not high.
1. Lack of security because its value is exposed in the URL address of the browser.
2. Objects cannot be passed.

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.

B)Possible problems
1. When the Resonse. QueryString function transmits Chinese character parameters, an error occurs that the specific value of the parameter cannot be completely passed. There are two solutions.

Method 1: You need to resetWeb. configInEncodingAnd global settings.

1. First line: <? Xmlversion = "1.0" encoding = "UTF-8"?>
Changed to: <? Xmlversion = "1.0" encoding = "GB2312"?>
2. <! -- Globalization This section sets the application globalization settings. -->
RequestEncoding = "UTF-8"
ResponseEncoding = "UTF-8"
<! -- Globalization This section sets the application globalization settings. -->
RequestEncoding = "GB2312"
ResponseEncoding = "GB2312"

Method 2: UseServer. UrlEncodeAndServer. UrlDecodeEncodes and decodes Chinese or special characters.


II,Use ApplicationA variable is a value passed between pages.

Application variables are valid throughout the Application lifecycle, similar to using global variables, so they can be accessed on different pages. The difference between it and Session variables is that the former is a global variable shared by all users, and the latter is a global variable unique to each user.
 For example:
The counter variable for Website access generally uses the Application variable. When multiple requests access this variable, you can operate on it. This variable can be directly used by all pages of the Application. The account name you log on to generally uses the Session variable. When multiple requests are accessed, they have their own Session variables and can only operate on their own Session variables, this variable is directly used on each page of the application to obtain basic user information.

(1) advantages and disadvantages

1. Simple to use and consume less server resources.
2. Not only simple data can be passed, but also objects can be passed.
3. The data size is unlimited.

1. Global variables are prone to misoperations.

(2) Usage


III,Use SessionVariable

Using the Application variable is the third way to pass values between pages. Session variables are very similar to Application variables, and their differences are also mentioned above.

(1) advantages and disadvantages
1. Simple to use, not only can pass simple data types, but also can pass objects.
2. The data size is unlimited.

1. storing a large amount of data in Session variables will consume a lot of server resources.

(2) Usage

1. Create the name and Value you need to pass in the Code on the Source Page to construct the Session variable: Session ["Nmae"] = "Value (OrObject )";

2. The code on the target page uses the Session variable to retrieve the passed value. Result = Session ["Nmae"]

IV,Use CookieObject

Cookie objects are the fourth way to pass values between pages. Cookies are used to store small pieces of information in a user's browser and store user-related information. For example, a user accesses a website.

User ID, user preferences, etc. The user can retrieve the previous information during the next visit. Therefore, cookies can also pass values between pages. Cookie in the browser through the HTTP Header

And the server. A Cookie can only contain string values. If you want to store an integer value in a Cookie, you must first convert it to a string.
You can retrieve all the cookies of all browsers by traversing the Cookie set of the Request object. The method is as follows:
Foreach (string strKey in Request. Cookies)
LblCookies. Text + = strKey + "=" + Request. Cookies [strKey]. Value;

(1) advantages and disadvantages

1. Easy to use, is a very common way to maintain the user status. For example, you can use a shopping website to maintain the user status when a user spans multiple page forms.

1. People are often criticized for being used to collect user privacy.

(2) Usage

1. Create the name and value you need to pass in the Code on the Source Page to construct the Cookie object:
HttpCookie objCookie = newHttpCookie ("myCookie", "Hello, Cookie! ");
Response. Cookies. Add (cookie );
2. The code on the target page uses the Cookie object to retrieve the passed Value: Result = Request. Cookies ["myCookie"]. Value;


V,Use Server. Transfer

Using the Server. Transfer variable is the fifth way to pass values between pages. The above four methods are often used in ASP, but this method is newly introduced in ASP. NET.

. Server. Transfer is to switch from the current ASPX page to the new ASPX page. The Server executes the new page and outputs it. In the new page, Context. Handler is used to obtain the previous page Transfer.

Values of various data types, form data, and QueryString. Because the redirection is completed on the server side, the URL address in the client browser will not change.
When Server. Transfer is called, the current ASPX page is terminated and the execution process is transferred to another ASPX page. However, the new ASPX page still uses the response stream created on the previous ASPX page.


Here we will compare the differences between Server. Transfer and Response. Redirect used in "1.
(1) Server. Transfer is completed on the Server, so the URL address in the browser of the client will not change; Response. Redirect is completed on the client, and it is proposed to the Server

The URL address in the browser of the client changes.
(2) The Server. Transfer is completed on the Server without the need for a request from the client. This reduces the number of requests sent from the client to the Server. [2]
(3) Server. Transfer can only jump to the page specified by the local virtual directory, that is, the page in the project, while Response. Redirect is very flexible and can jump to any

(4) Server. Transfer can upload values of the previous page to the new page. Response. Redirect can only use parameters in the URL or the above four methods.

Upload values of various types to a new page.

Continue to our Server. Transfer usage.

(1) advantages and disadvantages

1. Direct redirection on the server side, which is easy to use and reduces the number of requests sent from the client to the server.
2. Various data types and control values can be passed.

1. the URL address in the browser on the client is unchanged, which may cause unexpected problems on the new page. For example, if the source page and target page are not in the same virtual directory

Or its subdirectories. [3]

(2) Usage

1. In the code on the Source Page, use the Server. Transfer of the Page class to jump to another Page to Transfer Page data:
Server. Transfer ("destinationWebForm. aspx", "false ").

2. Use Context. Handler to receive data on the target page:
FormerPage formerPage = (FormerPage) Context. Handler;
Then, use the formerPage attributes and methods to obtain the value of the previous page, or directly use
Context. Items ["myParameter"]
To obtain the value of the previous page.

It must be noted that these values can be obtained correctly for various data types or control values on the previous page only when the new page is loaded for the first time. In the future postback, you cannot

Obtain the values of various data types or controls on the previous page. Because the current page instance is obtained at this time, you need to use if (!) in the Page_Load () event of the new page (destinationWebForm. aspx (! IsPostBack) contains the code for getting the value of the previous page to obtain the values of various data types, form data, and QueryString passed on the previous page.

VI,Use POST(This mode is used to pass the username and password set in the integrated logon)

First, let's briefly introduce getAnd post

L Get: obtains the information of the resource specified by the request URI in Entity mode. If the request URI is only a data generation process, in the end, the response object will return the resource to which the result of the processing process points, rather than the description of the processing process.

L Post: used to send a request to the target server, requiring it to accept the entity attached to the request and treat it as an additional sub-item of the resource specified by the request URI in the Request queue, post is designed to implement the following functions in a uniform way:

1. Explanation of existing resources

2. send messages to bulletin boards, newsgroups, email lists, or similar discussion groups.

3. submit data blocks

4. Expand the database through additional operations

From the above description, we can see that Get is a request to request data from the server, while Post is a request to submit data to the server, the data to be submitted is located in the entity behind the information header.

HTTPRequest: getAnd postDifferences Between Methods

L similarities;

Get and post (for "post" requests, unless the cache-control or expires header field indicates that the request cannot be cached) are cacheable;

L differences:

1. Get is to Get data from the server, and post is to send data to the server

2. get adds the parameter data queue to the URL specified by the action attribute of the submission form. The values correspond to each field in the form one by one. In the URL, we can see that post is implemented through the HTTP post mechanism, place the fields and content in the form in the html header and send them to the URL address referred to by the action attribute. You cannot see this process;

3. The size of data transmitted by get is small and cannot exceed kb. The size of data transmitted by post is large, which is not restricted by default. But theoretically, 2G

4. Low get security; high post security;

5. get applies to multiple requests, while retaining post is only used to update sites;

6. If the method is not specified during form submission,The default value isGetRequest, The data submitted in form will be appended to the url? Separate from URL. The letter and number characters are sent as they are, but spaces are converted to "+". Other symbols are converted to % xx, xx indicates the ASCII (or ISO Latin-1) value in hexadecimal notation;

7. The data submitted by the get request is placed in the HTTP Request Header, while the data submitted by the post is placed in the object data;


Differences between "post" and "get" in Forms

In form, post or get can be used. They are all valid values of method. However, the post and gei methods are at least two different in their applicability;

1. The get method transmits user input through URL requests. The Post method is in another form.

2. The get method must be used for submission.Request. QueryStrinG to get the value of the variable. When submitting the post method, you must use Request. Form to access the submitted content, such:

Request. Form ["Parameter Name "], Request ["Parameter Name "], Request. Param ["Parameter Name "]

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: 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.