State management is
very important and useful in ASP.NET. It is also asked in many interviews
to fresher and experienced developers.
State management is a preserve state control and object in
an application because ASP.NET web applications are stateless. A new instance
of the Web page class is created each time the page is posted to the server. If
a user enters information into a web application, that information would be
lost in the round trip from the browser (MSDN).
In a single line,
State management maintains and stores the information of any user till the end
of the user session.
Types of state
management
There are two types
of state management techniques: client side and server side.
Client side
1. Hidden Field
2. View State
3. Cookies
4. Control State
5. Query Strings
Server side
1. Session
2. Application
Server side
Session
Session is a very important technique to maintain state.
Normally session is used to store information and identity. The server stores
information using Sessionid.
Set User Session
protected void
btnSubmit_Click(object sender, EventArgs e)
{
Session["UserName"] =
txtName.Text;
Response.Redirect("Home.aspx");
}
Session Event
Session event can be
seen in project Global.asax file.
Two types of Session
Events
Session_Start
The Session_start event is raised every time a new user
requests without a session ID.
void Session_Start(object sender, EventArgs e)
{
Session["master"] = "~/Master.master";
}
Session_End
The Session_End event is raised when session is ended by a
user or a time out using Session end method.
void Session_End(object sender, EventArgs e)
{
Response.Write("Session_End");
}
The session is stored
in the following for ways in ASP.NET.
•InProcMode
It is a default session mode and a value store in web server
memory (IIS). In this the session value stored with server start and it ends
when the server is restarted.
•State Server Mode
In this mode session data is stored in separate server.
• SQL
Server Mode
In this session is stored in the database. It is a secure
mode.
• Custom
Mode
Generally under session data is stored in InProc, Sql
Server, State server, etc. If you store session data with other new techniques
then provide ASP.NET.
Application
Application State is a server side management state. It is
also called application level state management. In this mainly store user
activity in server memory and application event shown in Global.asax file.
There are three types
of applications in ASP.NET.
Application_Start
void Application_Start(object sender, EventArgs e)
{
Application["AppstartMessage"] = "Welcome to my blog";
}
This event begins with domain start.
Application_Error
In this section manage unhandled exception error.
void Application_Error(object sender, EventArgs e)
{
// Write an
unhandled error code exception
}
Application_ End
This ends with domain or restarts IIS.
void Application_End(object sender, EventArgs e)
{
Application["AppEndMessage"] = "Application Closed";
}
Cache
Cache is stored on server side. It implements Page Caching
and data caching. Cache is use to set expiration polices
Response.Cache.SetExpiresTime(DateTime.Now.AddDays(1));
Client Side
Now here I am explaining client side state management one by
one:
Also state management has the following four important parts
available on the client side,
Cookie
Cookie is a small and an important part of ASP.NET. In this
store user information, session and application. It can be created constant and
temporary and they work with browser request. Cookies are store on client side.
The server can read cookies and abstract data.
Two types of cookies
are available,
Persistence
This type of cookie works with Date and time.
Response.Cookies["CookieName"].Value = "Test Cookies";
//set
expire time
Response.Cookies["CookieName"].Expires = DateTime.Today.AddHours(1);
Non-Persistence
This is a temporary cookie. It is created with access
application and discards the close application.
Response.Cookies["CookieName"].Value = "Test
Cookies";
Hidden Field
Hidden fields are used to store value to client side. Hidden
field is not displayed on the browser, but it works on a request.
if (HiddenField1.Value != null)
{
int val = Convert.ToInt32(HiddenField1.Value) + 1;
HiddenField1.Value = val.ToString();
Label1.Text = val.ToString();
}
Viewstate
Viewstate is a very useful client side property. It is used
for page level state management. Viewstate stores any type of data and used for
sending and receiving information,
Viewstate is easy to apply and does not need access to any
server resources. In a Viewstate, do not store big data, only store small
values. Viewstate enables and disables on page level control. It also supports
Encryption and Decryption and data/value is stored in hashed format. So we are
not storing important data such as password, account information, etc. When
more data is stored in this, then the page becomes heavy.
Query String
Query string stores the value in URL.
Response.Redirect("ShowStringValue.aspx?Username="
+ txtUsername.Text);
No comments:
Post a Comment