Page pass-through method for ASP.

Source: Internet
Author: User
Tags server memory

Page pass-through is a problem that will be faced in the early days of the study, such as page pass value, store object value, Ajax, class, model, form, etc. But generally speaking, it is simpler to use Querystring,session,cookies,application,server.transfer.

  First, QueryString

QueryString is a very simple method of transmitting values that can be displayed in the browser's address bar. You can use this method if you are passing one or more security requirements that are not high or have a simple structure. However, you cannot use this method for passing arrays or objects.

Advantages of this approach: 1. Simple to use, it is very effective to pass a number or text value when the security requirements are not high.
Disadvantages of this approach: 1. Lack of security, because its value is exposed in the browser's URL address.
2. The object cannot be passed.

How to use: 1. Construct the URL address in the source page code with the name and value that you want to pass.
2. The code on the source page is Response.Redirect (URL) and redirected to the URL address above.
3. The code on the destination page uses the request.querystring["name"] and takes out the value passed in the URL address.

Example: (1) a.aspx

private void Button1_Click (object sender, System.EventArgs e) {string s_url;   S_url = "B.aspx?name=" + Label1.Text; Response.Redirect (S_url); }

(2) b.aspx

private void Page_Load (object sender, EventArgs e) {label2.text = request.querystring["name"];}

  Second, Session

  Presumably this is the most common usage of everyone, its operation is similar to application, acting on the user's personal, so, excessive storage will lead to the depletion of server memory resources.

Pros: 1. Simple to use, not only to pass simple data types, but also to pass objects.
2. The size of the data volume is unlimited.

Cons: 1. Storing large amounts of data in session variables consumes more server resources.

2. Easy to lose.

How to use: 1. Create the name and value you need to pass in the code of the source page construct the Session variable: session["name"]= "Value (Or Object)";

2. The code in the destination page uses the session variable to remove the passed value. Result = session["Nmae"]

Note: The session can be destroyed when not used, the method is: Clear one: Session.remove ("Session name");

Clear all: Session.clear ();

Example: (1) a.aspx

private void Button1_Click (object sender, System.EventArgs e) {session["name"] = Label.text;}

(2) b.aspx

private void Page_Load (object sender, EventArgs e) {string name; Name = session["Name"]. ToString (); }

Third, cookies

  This is also a common use of the method, the cookie is used to store small pieces of information in the user's browser, save the user's relevant information, such as user access to a website user ID, user preferences, etc., the next time the user can retrieve the previous information. So cookies can also pass values between pages. Cookies are passed back and forth between the browser and the server via an HTTP header. A cookie can contain only the value of a string, and if you want to store an integer value in a cookie, you need to first convert to a string.

As with the session, it is for each user, but there is an essential difference, that is, the cookie is stored on the client, and the session is stored on the server side. And the use of cookies should be used in conjunction with the ASP.

Pros: 1. Simple to use, is a very common way to maintain user state. For example, in a shopping site, users can use it to maintain user status when they cross multiple page forms.

Cons: 1. It is often criticized for collecting user privacy.

2. Security is not high, easy to forge.


How to use: 1. Create a Cookie object in the source page code that creates the name and value you want to pass:

2. The code on the destination page uses the cookie object to remove the value passed: Result = request.cookies["MyCookie"]. Value;

Example: (1) a.aspx

private void Button1_Click (object sender, System.EventArgs e) {HttpCookie Objcookie = new HttpCookie ("MyCookie", "hello,c  Ookie! "); RESPONSE.COOKIES.ADD (Objcookie); }

(2) b.aspx

String myname1value;myname1value = request.cookies["MyCookie"]. Value;

  Iv. Application

The Application object is scoped to the whole global, which means it is valid for all users. It is valid throughout the application life cycle, similar to using global variables, so it can be accessed from different pages. It differs from the session variable in that the former is a global variable shared by all users, and the latter is a unique global variable for each user.

One might ask, since all users can use the application variable, where can he use it? Here's an example: the number of site visits. It can be manipulated when multiple requests are accessed.

Pros: 1. Easy to use, consumes less server resources.

2. Not only can pass the simple data, but also can pass the object.

3. The size of the data volume is unlimited.

Cons: 1. It is easy to be manipulated as a global variable. Therefore, the variables used by individual users are generally not application.

How to use: 1. Create the name and value you need to pass in the code of the source page construct the application variable: application["Nmae"]= "Value (Or Object)";

2. The code on the destination page uses the application variable to remove the passed value. Result = application["Nmae"]

Note: The common lock and unlock methods are used to lock and unlock, in order to prevent concurrent modifications.

Example: (1) a.aspx

private void Button1_Click (object sender, System.EventArgs e) {application["name"] = Label1.Text;}

(2) b.aspx

private void Page_Load (object sender, EventArgs e) {string name;   Application.Lock (); Name = application["Name"].   ToString (); Application.UnLock (); }

  Wu, Server.Transfer

  This can be said to be the method used by the development of the face-like object, which uses the Server.Transfer method to direct the process from the current page to another page, the new page uses the reply stream of the previous page, so this method is completely object-like, concise and effective.

Server.Transfer is to go from the current ASPX page to the new ASPX page, the server side executes the new page and outputs, and in the new page through Context.Handler to get the values of the various data types passed by the previous page, form data, QueryString. Because the redirection is complete on the server side, the URL address in the client browser does not change. When Server.Transfer is called, the current ASPX page terminates execution and the execution process goes to another ASPX page, but the new ASPX page still uses the reply stream created by the previous ASPX page.

PS: Compare the difference between Server.Transfer and Response.Redirect.
(1) Server.Transfer is done on the server side, so the URL address in the client browser will not change, and the URL address in the client browser will change as the client completes and the new page processing request is made to the server side.
(2) The Server.Transfer is done on the server side, without requiring the client to make requests, reducing the client's request to the server side. [2]
(3) Server.Transfer can only jump to the page specified in the local virtual directory, that is, the page in the project, and Response.Redirect is flexible enough to jump to any URL address.
(4) Server.Transfer can upload various types of values from the previous page to the new page, Response.Redirect can only use the URL with parameters or combined with the above four ways to upload various types of values to the new page.

Advantages: 1. Direct in the server end multiplicity orientation, easy to use, reduce the client to the server side request.

2. You can pass values for various data types and values for controls.

Cons: 1. The URL address in the client browser is not changed, which can cause some unexpected problems in the new page. For example, if the source page and the destination page are not in the same virtual directory or its subdirectories, the use of relative path of the picture, hyperlink will lead to an error point.

How to use: 1. In the source page code, use the page class's server.transfer to jump to another page to pass the paging data: Server.Transfer ("B.aspx", "false").

2. On the destination page, use Context.Handler to receive data: formerpage formerpage = (formerpage) Context.Handler; Then use the properties and methods of the formerpage to get the value of the previous page, or use the context.items["Myparameter" directly)

Example: (1) a.aspx

public string Name {get{return label1.text;} private void Button1_Click (object sender, System.EventArgs e) {Serve R.transfer ("b.aspx"); }

(2) b.aspx

private void Page_Load (object sender, EventArgs e) {a newweb;//instance a form Newweb = (source) Context.Handler;   String name; name =; }

The above is commonly used in several methods of page transfer, I generally use the session and querystring to pass the value, a few cases will be used to cookies. This article is only to introduce the use of these methods, the internal principle does not have too much explanation, about the session storage mode:

1. Configure the session in Web. config

Session configuration information in the Web. config file:

Private void   Page_Load (object//   newweb = (source) Context.Handler;  string=;}

mode sets where to store the session information:

-off is set to not use session function;

-inproc is set to store the session in the process, that is, the ASP storage mode, which is the default value;

The-stateserver is set to store the session in a separate state service;

The-sqlserver setting stores the session in SQL Server.

cookieless sets where the client's session information is stored:

-ture uses the cookieless mode, when the client's session information is no longer stored using a cookie, but instead is stored via a URL. For example, the URL is http://localhost/MyTestApplication/(ulqsek45heu3ic2a5zgdl245)/default.aspx

-false uses cookie mode, which is the default value.

Timeout sets the number of minutes after which the server automatically discards session information. The default is 20 minutes.

stateConnectionString Sets the server name and port number used when the session information is stored in the State service, for example: "tcpip=". When the value of mode is StateServer Yes, this property is required. (42424 is the default port).

sqlConnectionString sets the connection string when connecting to SQL Server. For example, "Data source=localhost;integrated security=sspi;initial catalog=northwind". This property is required when the value of mode is SQL Server.

stateNetworkTimeout settings when the session state is stored using StateServer mode, the TCP/IP connection to the server that stores the state information is disconnected after the number of seconds that the Web server is idle. The default value is 10 seconds.

Here's how to store the session using StateServer and SQL Server

2.1 StateServer

The 1th step is to turn on the state service. Open the Control Panel → administrative tools → services command and locate the ASP. Right-click Service selection to start.

If you formally decide to use the status service to store the session, don't forget to modify the service to self-boot (the service can start itself after the operating system restarts) so as not to forget to start the service and cause the website session not to use

2nd step, join in system.web node: statenetworktimeout= "> stateConnectionString" indicates the communication address (IP: Service port number) of the state server. Since we are now testing this machine, we set up the cost machine address here. The default listening port for the status service is 42422. Of course, you can also modify the port number of the status service by modifying the registry.

(To modify the registry to modify the port number of the state service: Enter regedit in the run to start Registry Editor-turn Hkey_local_machinesystemcurrentcontrolsetservicesaspnet_ Stateparameters node, double-click the Port option-Select Base to Decimal, and then enter a port number. )

2.2 SQL Server

Execute a script file called InstallSqlState.sql in SQL Server. This script file will create a database in SQL Server dedicated to storing session information, and a SQL Server Agent job that maintains the session information database. We can find the file in the following path:

[System drive]\winnt\\framework\[version]\

Then open Query Analyzer, connect to the SQL Server server, open the file you just made and execute. Wait a moment, the database and the job is set up. At this point, you can open Enterprise Manager and see a new database called ASPState.

Change the value of mode to SQL Server. Note that you also need to modify the value of the sqlconnectionstring at the same time, in the format: sqlconnectionstring= "Data source=localhost; Integrated Security=sspi; " (This is through Windows Integrated authentication)

2. Traverse and Destroy session

4.1 Traversal:

System.Collections.IEnumerator sessionenum = Session.Keys.GetEnumerator ();
while (Sessionenum.movenext ())
Response.Write (Session[sessionenum.current.tostring ()). ToString () + "");

4.2 Destruction: Session.Abandon ().

Page pass-through method for ASP.

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.