Asp. NET page pass-by-value comparison

Source: Internet
Author: User

As an ASP, especially for the B/s development, the transfer of variable values between different pages is very extensive, and it is necessary to master the differences and characteristics between different ways. This article will make a brief summary of this knowledge point.

The transfer of values between pages can be broadly divided into the following: pass through the Get method, via post, through the session, through the cookie way and, through the application way, through the cache way, through the way of the page property and through the static variable. Here's a comparison of the above:

1, get way: Through the Get method between the page value is very common, the usage is very simple, just need to pass the parameters attached to the URL behind, such as: To access the current page, http://www.cnblogs.com/xsyblogs/admin/ Editposts.aspx?opt=1, we can see that at the end of the URL there is a "opt=1" word, which is the argument we want to pass. On the current page, if we want to get this parameter, we can get the passed parameter value by simply Request.QueryString ("opt") method. Because it is attached to the URL, so its security is conceivable, but for like passing the page number to get, to get the ID of the article such parameters are very useful. A fatal flaw in Get mode is that you can only pass 255 characters at a time, but it is also useful for simple, small amounts of data.

2, Post mode: With the most in addition to get is the post, compared to get, post seems more secure, it by the need to submit the data encapsulated in the hidden domain, although a little bit of development of friends know that post is submitted can also be viewed through the browser development tools, But for our usual use, it's enough. If you need to submit data by post, you must have the name attribute, which is obtained through Request.Form ("keyword") on the server side. The Post method works for big data, including file uploads.

The difference between the Get and post methods gives a slight contrast here: see.

3. Session mode

Session mode is also a common method of saving page values, which stores user data on the server side, storing an ID on the client as a token that is validated with the service side. We can put the username in session, so that we can determine the value of a key in the session to determine whether the user is logged in, the user name is how much. ASP. Can set the session storage mode, location, whether the SessionID is dependent on the cookie. There are three ways to store the session:

Inproc (default), session is stored in the IIS process.

The stateserver,session is stored in a separate Windows service process (which can be a Web server).

The sqlserver,session is stored in a table in the SQL Server database (SQL Servers).

Although the session of the InProc mode is stored directly in the Web server IIS process, it is faster, but each restart of IIS causes the session to be lost. Using the latter two modes, we can completely separate the session from the Web server, thus reducing the pressure on the Web server and reducing the probability of session loss.

Therefore, SessionID is stored on the client side (which can be a cookie or a URL), and the rest is stored on the server (either an IIS process, a stand-alone Windows service process, or a SQL Server database).

The session schematic diagram is as follows:

4, Application Way

Storing data in application, equivalent to a global variable, shares the entire site data, is valid throughout the life of the application, and applies to all pages, all users share the data. The method is similar to the session, and it is also a key value pair to take the form of values and assignments.

5. Cookie Mode

Cookies are a classic way to keep data on the client side. But because it is stored on the client, its security is low and is limited by the client settings. Here's a look at the cookie schematic:

6. Cache mode

The cache is not widely used here, but it also has this function. The cache mainly stores the user data in the server data cache, can store the object directly, but the data update is not timely. Usage: cache["username"]= "Xiaosy";

7. Page Properties

The page properties described here refer to the use of HttpContext to obtain information about the requested page. We often say that the page class object, since through the HttpContext to the page class objects have been taken, and what worry can not get inside the variable it? But one thing to be aware of in this way is that the type of coercion must be correct, otherwise it will not get anything. Here are two examples:

Gets the value of the control:

Send page <input type= "button" id= "Generatethum" value= "Generate"/>    <asp:textbox id= "TxtBox1" runat= "Server" Text = "Xiaosy" ></asp:TextBox>    <asp:button runat= "Server" id= "Btnbutton" text= "go to another page" onclick= "Btnbutton _click "/> protected void Btnbutton_click (object sender, EventArgs e)        {            Server.Transfer (" webform2.aspx ");        } Receive page <asp:label runat= "server" id= "Label1" ></asp:label>protected void Page_Load (object sender, EventArgs e)        {            WebForm1 WebForm1 = (WebForm1) Context.Handler;            Label1. Text = ((TextBox) Webform1.findcontrol ("TxtBox1")). Text;        }

Get Public variables:

The page being sent is public  string userName = "Xiaosy";        protected void Btnbutton_click (object sender, EventArgs e)        {            Server.Transfer ("webform2.aspx");        } The received page if (Context.Handler is WebForm1)            {                WebForm1 WebForm1 = (WebForm1) Context.Handler;                This.label1.Text = webform1.username;//Get public variable            }

You can also get the value in this way:

The page sent protected void Btnbutton_click (object sender, EventArgs e)        {            context.items["name"] = Txtbox1.text;            Server.Transfer ("webform2.aspx");        } Received page protected void Page_Load (object sender, EventArgs e)        {            if (Context.Handler is WebForm1)            {                This.label1.Text = context.items["Name"]. ToString ();            }                   }

  8. Static variable mode

A static variable is one that declares a variable, assigns a value, and then takes the value of the variable when it is needed. Here is not an example.

9, ViewState

ViewState is an ASP. NET-specific page retention mechanism used to restore the state of a page, and stateless HTTP requests become stateful because of the presence of viewstate. ViewState mainly consists of serializing the page controls and their stored data in a hidden field named _viewstated. Mainly applies to the page postback needs to save the data, the data is too big also can affect the page sends the efficiency. The schematic diagram is as follows:

Asp. NET page pass-by-value comparison

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.