-----------------------------------------------------------

New Message on MumbaiUserGroup

-----------------------------------------------------------
From: Swapnil_B1
Message 1 in Discussion

  Folks,   It is something very common and we are using some of the features of 
ASP.NET like State Management in Web Pages. Since we use these features on day 
to day basis, it becomes very essential to get clear understanding of it. Let 
me highlight in detail and the various ways of Managing State in ASP.NET.  
Web form pages are HTTP-Based, they are stateless, which means they don’t know 
whether the requests are all from the same client, and pages are destroyed and 
recreated with each round trip to the server, therefore information will be 
lost, therefore state management is really an issue in developing web 
applications.

We could easily solve these problems in ASP with cookie, query string, 
application, session and so on. Now in ASP.NET, we still can use these 
functions, but they are richer and more powerful, so let’s dive 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 will 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 browser 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 application 
first to detect if the user has logined before, we can retrieve the user 
informatin from cookies:

 
if (Request.Cookies[“username”]!=null)
lbMessage.text=”Dear “+Request.Cookies[“username”].Value+”, Welcome shopping 
here!”;
else
lbMessage.text=”Welcome shopping here!”;

If you want to store client’s information, you can use the following code:
Response.Cookies[“username’].Value=username;

So next time when the user request the web page, you can easily recongnize the 
user again.  
B. Hidden Field  
A hidden field does not render visibly in the browser, but you can set its 
properties just as you can with a standard control. When a page is submitted to 
the server, the content of a hidden field is sent in the HTTP Form collection 
along with the values of other controls. A hidden field acts as a repository 
for any page-specific information that you would like to store directly in the 
page. Hidden field stores a single variable in its value property and must be 
explicitly added it to the page.
ASP.NET provides the HtmlInputHidden control that offers hidden field 
functionality.

 
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 order to use hidden field, you have to use HTTP-Post 
method to post web page. Although its name is ‘Hidden’, its value is not 
hidden, you can see its value through ‘view source’ function.  
C. View State  
Each control on a Web Forms page, including the page itself, has a ViewState 
property, it is a built-in struture for automatic retention of page and control 
state, which means you don’t need to do anything about getting back the data of 
controls after posting page to the server.

Here, which is useful to us is the ViewState property, we can use it to save 
information between round trips to the server.

 
//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 compressed and encoded.

 
D. Query Strings  
Query strings provide a simple but limited way of maintaining some state 
information.You can easily pass information from one page to another, But most 
browsers and client devices impose a 255-character limit 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 be an issue. 
A URL with query strings may 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 be 
obtained by using the following codes:

 
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 will never 
get the value from query strings.  
We will see Server Side State management in next article  
Hope this is useful to you all  
Swapnil (swaps)  
http://swapsnet.spaces.live.com/

-----------------------------------------------------------

To stop getting this e-mail, or change how often it arrives, go to your E-mail 
Settings.
http://groups.msn.com/MumbaiUserGroup/_emailsettings.msnw

Need help? If you've forgotten your password, please go to Passport Member 
Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help

For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact

If you do not want to receive future e-mail from this MSN group, or if you 
received this message by mistake, please click the "Remove" link below. On the 
pre-addressed e-mail message that opens, simply click "Send". Your e-mail 
address will be deleted from this group's mailing list.
mailto:[EMAIL PROTECTED]

Reply via email to