Variable numerical management in asp.net------look at this. I basically passed the variable to the original Request.Form. Despair.

Source: Internet
Author: User
Tags configuration settings file system functions http cookie session id variables unique id client
asp.net|request| variable Web form pages are http-based, they are stateless, which means they don ' t know whether the requests All from the same client, and pages are destroyed and recreated with each round trips to the server, therefore information Would be lost, therefore state management is really a issue in developing Web applications

We could easily solve these problems into ASP with cookies, query string, application, session and. Now in asp.net, we are still can use this functions, but they are, richer and more powerful into it.

Mainly there are two different ways to manage Web page ' s state:client-side and server-side.

1.client-side State Management:
There is no information maintained on the server between round trips. Information would be stored in the page or on the client's computer.

A. Cookies.

A cookie is a small amount of data stored either in a text file on the client ' s file system or In-memory in the client bro Wser session. Cookies are mainly used for tracking data settings. Let's take an Example:say we want to customize a welcome Web page, when the user request the default Web page, the Applic ation the detect if the user has logined before, we can retrieve the user informatin from cookies:
[C #]
if (request.cookies["username"]!=null)
lbmessage.text= "Dear" +request.cookies["username"]. Value+ ", Welcome shopping here!";
Else
lbmessage.text= "Welcome shopping here!";

If you are want to store client ' s information, your can use the following code:
[C #]
response.cookies["Username '". Value=username;

So next time the user request the Web page, can easily recongnize the user again.



B. Hidden Field

A hidden field does not render visibly the browser, but can set its properties just as and can with a standard cont Rol. When a page was submitted to the server, the content of a hidden the field was sent in the HTTP Form collection along with the V Alues of the other controls. A hidden field acts as a repository for any page-specific information, you would like to store directly in the page. Hidden field stores a single variable in its Value property and must is explicitly it to the page.
asp.net provides the HtmlInputHidden control that offers hidden field functionality.
[C #]
protected System.Web.UI.HtmlControls.HtmlInputHidden Hidden1;
To assign a value to Hidden field
Hidden1.value= "This is a test";
To retrieve a value
String Str=hidden1.value;

Note:keep in mind, in the order of hidden field, you are have to the use Http-post method to Post Web page. Although its name is ' Hidden ', the its value isn't Hidden, you can have a ' view source ' function.


C. View State

Each of the Web Forms page, including the page itself, has a ViewState property, it's a built-in struture for Autom atic retention of page and control state, which means you don ' t need "do anything" about getting back the data of s after posting page to the server.

Here, which are useful to us are the ViewState property, we can use it to save information between round trips to the server .
[C #]
To save information
Viewstate.add ("Shape", "circle");
To retrieve information
String shapes=viewstate["shape"];

Note:unlike Hidden Field, the values in ViewState are invisible when ' view source ', they are and compressed.



D. Query Strings

Query strings provide a simple but limited way of maintaining the state some. can easily pass information from one page to another, But most browsers and client devices impose a 255-character Limi T on the length of the URL. In addition, the "query values are exposed to the" Internet via the URL so in some cases security may is an issue.
A URL with query strings could look like this:

Http://www.examples.com/list.aspx?categoryid=1&productid=101

When List.aspx is being requested, the category and product information can are obtained by using the following codes:
[C #]
String CategoryID, ProductID;
Categoryid=request.params["CategoryID"];
Productid=request.params["ProductID"];

Note:you can only use Http-get method to post the Web page, or you'll never get the value from query strings.

2. Server-side State Management:
Information'll is stored on the server and it has higher security but it can use more Web server resources.



A. Aplication Object

The Application object provides a mechanism for storing data, is, accessible to all code running within the Web Applica tion, the ideal data to inserts into application state variables are data that are shared by multiple sessions and does not C Hange often. And just because it is visible to the entire application, with need to used Lock and UnLock pair to avoid have conflit VA Lue.

[C #]

Application.Lock ();

application["MyData"]= "MyData";

Application.UnLock ();


B. Session Object

Session object can is used for storing session-specific information which needs to be maintained between server round trips and between requests for pages. Session object are per-client basis, which means different clients generate the session object. The ideal data to store in Session-state variables is short-lived, sensitive data this is specific to a individual Sessio N.

Each active ASP.net sessions is identified and tracked using a 120-bit SessionID string containing Url-legal ASCII characte Rs. SessionID values are generated using a algorithm that guarantees of so, uniqueness does not sessions, and collide D ' s randomness makes it harder to guess session ID of a existing session.
SessionIDs are communicated across Client-server requests by the HTTP cookie or a either URL, modified on how to Set the application ' s configuration settings. How do I to set the session setting in application configuration? Ok, let's go further.

Every Web application must have a configuration file named Web.config, it is a xml-based file, there be a section name ' SE Ssionstate ', the following is a example:

<sessionstate mode= "InProc" stateconnectionstring= tcpip=127.0.0.1:42424 "sqlconnectionstring=" Data source= 127.0.0.1;user id=sa;password= "cookieless=" false "timeout="/>

' cookieless ' option can be ' true ' or ' false '. When it is ' false ' (default value), asp.net'll use HTTP cookies to identify users. When it is ' true ', asp.net'll randomly generate a unique number and put it just right ahead to the requested file, this Number is used to identify users, and you can have a it on the address bar of IE:

http://localhost/Management/(2YZAKZEZ3EQXUT45UKYZQ3QP)/default.aspx

Ok, it is further enough and let's go back to session object.
[C #]
To store information
session["MyName"]= "Mike";
To retrieve information
myname=session["MyName"];


C. Database

Database enables your to store large amount of information pertaining to the state in your WEB application. Sometimes users continually query the database by using the unique ID, your can save it in the database for use across mult Iple request for the pages in your site.



Summary

ASP.net has more functions and utilities than ASP to enable your to manage page state more efficient and effective. Choosing among the options would depand upon your application, you are have to I about the following before making any Choo Se

How much does information do your need to store?
Does the client accept persistent or in-memory cookies?
Do you want to store the information on the client or server?
Is the information sensitive?
What kind of performance experience are you expecting from your pages?
Client-side State Management Summary

Method
Use when

Cookies
You are need to store small amounts of information on the client and security are not a issue.

View State
You are need to the store small amounts of information for a page that would post back to itself. Use the ViewState property does supply semi-secure functionality.

Hidden fields
You are need to store small amounts of information for a page that would post back to itself or another page, and security is n OT an issue.

Can use a hidden field in pages that are submitted to the server.

Query string
You are are transferring small amounts of information from one page to another and security are not a issue.

Note To can use query strings only if you are requesting the same page, or another page via a link.



Server-side State Management Summary

Method
Use when

Application state Object
You are storing infrequently changed, application-scope information-is-used by many users, and security isn't ' ISS Ue. Does not store large quantities of the information in a application state object.

Session state Object
You are storing short-lived information this is specific to a individual session, and the security are an issue. Do not store large quantities of information in a session state object. Being aware that a sessions state object would be created and maintained for the lifetime of every sessions in your application. In applications hosting many users, this can occupy significant server resources and affect.

Database Support
You are storing large amounts of information, managing transactions, or the information must survive application and Sessi On restarts. Data Mining is a concern, and the security are an issue.



Developing asp.net programme is really funny, once to it, you can jump to it. Next time let us talk about another topic:cache. Enjoy. net!!



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.