Vous êtes sur la page 1sur 4

STATE MANAGEMENT

1- State management is the process by which you maintain the state and page info
rmation over multiple requests for the same or different pages.
2- As is true for any HTTP-based technology, Web Forms pages are stateless, whic
h means that they do not automatically indicate whether the requests in a sequen
ce are all from the same client or even whether a ssingle browser instance is st
ill actively viewing a page or site
3- Firthermore, pages are destroyed and re-created with each round trip to the s
erver; therefore, page information will not exist beyond the life cycle of a sin
gle page.
Server side:
1: Session
==> Use
-To Write
Session["Name"] = TextBox1.Text; // this sets the name variable of curre
nt session
- To read
Lablel1.Text = Session["Name"].ToString();
In web.config,
add under system.web
<sessionState mode="InProc" timeout="1" cookieless="true" regenerateExpi
redSessionId="true" > //session expires in 1 minute
Session state stored inside ASP.NET's worker process
mode="StateServer" stateConnectionString="tcpip=IpAddressOfServer"
Session state stored in ASP.NET state service
mode="SQLServer" sqlConnectionString="server=orion;integrated security=t
rue"
Created with InstallSqlState.sql or InstallPersistSql
==> Session Events
-Session_Start
-Session_End
//In .NET Default timeout for session in 20 minutes
can be changed with timeout="" in sessionState
2: Application
One of the ways to store some data on the server and faster than accessi
ng the database. The data stored on the Application State is available to all th
e users(Sessions) accessing the application. So application state is very useful
to store small data that is used axross the application and same for all the us
ers.
Stores data at application level and this is available to all the users
Available until explicitly removed or overridden or server is closed

Should only be used when data is limited, if the data becomes very large
it chokes the webserver
==> Use
We need to add a class that is Global Application Class (.asax), this cl
ass inherits System.Web.HttpWebApplication class
It has a lot of predefined events.
In Application_Start event,
Application["Count"] = 0;
In Session_Start,
Application["Count"] = Convert.ToInt32(Application["Count"]) + 1;
To display, in any normal page:
Label1.Text = Application["Count"].ToString();
To access in a webpage
Application["Name"] = TextBox1.Text;
Client Side:
1: View State
==>Advantages:
* No load on server.
* Simple implementation.
* Stores the values in hashed and compressed format.
==>Disadvantages:
* Performance consideration on client side as client res
ources are used.(Bandwidth is often a limitation).
* Device limitatio, mobile devices may not have enough m
emory.
* Potential security risk as he values stored in hashed
format can be still tampered with.
==> to use:
// manual use
*create a form with some fields
*create the event for button
in that event
:
:
ViewState["Name"] = TextBox1.Text;
Literal1.Text = ViewState["Name"].ToString(); // this will displ
ay the value of viewstate in literal
:
:
2: Control State
3: Hidden Fields

4: Cookies
- Mechanism for persisting textual data
^ For relatively small pieces of data
- HttpCookie class encapsulates cookies
- HttpRequest.Cookies collection enables to be read from request
- Two types:
a) Temporary cookies : Expire as soon as browsing sessio
n ends
b) Permanent cookies : Expires as per the expiry time se
t in the cookie
==> use
^ To create a cookie
In the button event:
HttpCookie cookie1 = new HttpCookie("Myookie");
cookie1["Name"] = TextBox1.Text;
Response.Cookies.Add(cookie1);
Response.Redirect("WebForm15.aspx");
^ To read a cookie
In page load
HttpCookie cookie1 = Request.Cookies["MyCookie"]; // we
do not use new as the cookie is already created
// use exception handlind to check the presence of cooki
es
Label1.Text = cookie["Name"].ToString();
// to set expiry of cookies
cookie1.Expirees=DateTime.Now.AddMinutes(1); // cookie e
xpires after 1 minute of creation
==> HttpCookie Properties
Name - cookie name
Value - cookie value
Values - Collection of cookie value
HasKeys - True if cooie contains multiple values
Domain - Domain to transmit cookie to
Expires - Cookie's expiration date and time
Secure - True if cookie should only be transmitted over
HTTPS
Path - Path to transmit
5: Query Strings
- A query string is information that is appended to the end of a
page.

- You can use query string to submit data back to your page or t
o another page through URL. Query strings provide a simple but limited way of ma
intaining some state information.
-Security sensitive information is not passed through this
==>Advantages
* No server resources are required. The query string is
contained in the HTTP request for a specific URL.
* Widespread support, almost all web browsers and client
devicces support using query strings to pass values.
* Simple Implementation, ASP.NET provides full support
==> Disadvantages
* Values are visible in the URL
* Limited Capacity limited by the max length of URL gene
rally, 2083 characters
==> use:
First add a page to redirect to
Inside the button click event:
Response.Redirect("WebForm15.aspx?nm="+TextBox1.Text+"&id="+Text
Box2.Text+""); //? signifies the start of query string : format of query string
& is separator
In the other page:
Add a label control, then in page load event
Label1.Text = Request.QueryString["nm"].ToString() +"<br />" + R
equest.QueryString["id"].ToString();

Vous aimerez peut-être aussi