0% found this document useful (0 votes)
2 views

aspnet5

The document provides an overview of state management in ASP.NET, which is essential for preserving user data in stateless web applications. It discusses two main types of state management: client-side (including View State, Cookies, and Query Strings) and server-side (including Session State and Application State). Each method is explained with definitions, features, and code examples to illustrate their usage in web development.

Uploaded by

ritenpanchasara
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

aspnet5

The document provides an overview of state management in ASP.NET, which is essential for preserving user data in stateless web applications. It discusses two main types of state management: client-side (including View State, Cookies, and Query Strings) and server-side (including Session State and Application State). Each method is explained with definitions, features, and code examples to illustrate their usage in web development.

Uploaded by

ritenpanchasara
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

SHREE SWAMI VIVEKANAND COLLEGE

SURENDRANAGAR
CLASS:- BCA/BSCIT SEMESTER -5
SUBJECT:- Asp.net
Prepared By:- Bhartiba parmar
Chapter-3
State Managment
Contents
• What is State management?
• Types of state management.
Client Side State management
Server Side State management
What is state management?
• ASP.NET 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.
• In a single line, State management maintains and stores the information of any
user till the end of the user session.
• State management in ASP.NET can be classified into
1. Client-side state management
2. Server-side state management
1. Client-side state management
• Client-Side State Management techniques are,
View State
Cookies
Query Strings
View State
• sometimes in ASP.NET applications the user wants to maintain or store their data
temporarily after a post-back.. In this case VIEW STATE is the most used and
preferred way of doing that.
• This property is enabled by default but we can make changes depending on our
functionality, what we need to do is just change the EnableViewState value to
either TRUE for enabling it or FALSE for the opposite operation.
• Some of the features of view state are:
It is page-level State Management
Used for holding data temporarily
Can store any type of data
Property dependent
Example:-
// Page Load Event
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
if (ViewState["count"] != null)
{
int ViewstateVal = Convert.ToInt32(ViewState["count"]) + 1;
View.Text = ViewstateVal.ToString();
ViewState["count"]=ViewstateVal.ToString();
}
else
{
ViewState["count"] = "1";
}
} lLabel Button
}

// Click Event
protected void Submit_click(object sender, EventArgs e)
{
View.Text=ViewState["count"].ToString();
}
Cookies
• Cookies is a small pieces of text information which is stored on user hard drive
using users browser for identify users. It may contain username, ID, password or
any information. Cookie does not use server memory.
• Cookies stored user computer at “C”\Document and Setting\Current
login_User\Cookie”.
• The Cookie is limited to small size and can be used to store only 4 KB (4096 Bytes)
text.
• Types of Cookies
Persistence Cookie
• This types of cookies are permanently stored on user hard drive.Cookies which
have an expiry date time are called persistence cookes. This types of cookies
stored user hard drive permenently till the date time we set.
Non-Persistence Cookie
• This types of cookies are not permanently stored on user hard drive. It stores the
information up the user accesng the same browser. When user close the browser
the cookies will be automatically deleted.
Example:-
protected void Button1_Click(object sender, EventArgs e)
{
Response.Cookies["name"].Value = TextBox1.Text;
Response.Cookies["name"].Expires = DateTime.Now.AddMinutes(1);
Label1.Text = "Cookie Created";
TextBox1.Text = "";
}

protected void Button2_Click(object sender, EventArgs e)


{
if (Request.Cookies["name"] == null)
{
TextBox2.Text = "No cookie found";
}
else
{
TextBox2.Text = Request.Cookies["name"].Value;
}
}
}
Query Strings
• Query string is a simple way to pass some information from one page to
another. The information can be easily pass to one page to another or to
same page. With query string method the information passed in url of page
request.
• This method many browsers supports only 255 character length of url in
query string. The value passed will be visible so some time it cause security
issue.
• For send information to other page Response.Redirect() method used and
for retrieve information from url use Request.QueryString() method used.
• Syntax of Query String
• Send information to other page
Response.Redirect(“nextpage.aspx?name=value”);
• Retrieve information from other page
Request.QueryString[“name”].ToString();
Example:-
• Write below code on SEND button click events on first page for pass information
to other page.
protected void Btnjump_Click(object sender, EventArgs e)
{
Response.Redirect("Default2.aspx?name=" + TextBox1.Text + "&age=" +
TextBox2.Text);
}

• Write below code on Retrieve button for retrieve information from url and display
it in label on Nextpage.aspx.
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "your name is" + Request.QueryString["name"];
Label2.Text = "your age is" + Request.QueryString["age"];
}
2. Server-side state management
• Server-Side State Management techniques are,
Session State
Application State
Session State
• Session state is a period of time to visit a website for a particular user. Session can
store the client data on a server. Session is a best state management features to
store the client data on server separately for each user.
• In simple word we can say, At the same time more than one users login to system,
all user identification name store separately until they logout. session can store
username or any unique identification of user for a login period time
• Session value can be accessible from all pages from website. We can store some
information in session in one page and can access same information on rest of all
page by using session.
• Syntax of session
Session[“session_name”] = “session value”;
• Declare session in asp.net
Session[“name”]=”svcollege”;
Response.Redirect(“nextpage.aspx”);
• Retrieve session value on other page
string myvalue= Session[“name”].ToString();
Response.Write(“Name = ” + myvalue);
• Important methods of Session object
Abandon: It is used to end a user session.
Clear: It clears all items from Session state.
Remove: This method is used to remove a particular item from Session state.
Timeout: If a user does not request a page of the application within certain
minutes then the session expires.
Example:-
• first page “default.aspx” on button click events.
protected void btnsession_Click(object sender, EventArgs e)
{
Session["name"] = txtname.Text;
Session["city"] = txtcity.Text;
Response.Redirect(“Default2.aspx");
}
• On “next.aspx” page we retrieve and display session value on web page using page_Load
events.
protected void Page_Load(object sender, EventArgs e)
{
string sname, scity;
sname = Session["name"].ToString();
scity = Session["city"].ToString();
Response.Write("welcome" + sname );
Response.Write("<br> your City " + scity);
}
Application State
• Application state is a global storage mechanism that used to stored data on the
server and shared for all users, means data stored in Application state is common
for all user. Data from Application state can be accessible anywhere in the
application.
• Store information in application state
Application[“name”] = “svcollege”;
• Retrieve information from application state
string str = Application[“name”].ToString();
Global.asax file:
• File stores information that applies to the entire web site.
• This file is used to declare application-level events and objects.
• It is an option file.only one file per website.
• It contains the code that is executed when certain events,such as start of an
application or error in an application.
• This is a global Application class, where we can set the code for the events given
below.
EVENT TYPES EVENT DESCRIPTIONS
Application Start This event is called, when application getting start.
Application End This event is called before an Application ends.
Application Error This event is called, when an un-handled error occurs.
Session Start This event is called, when a new session is starts.
Session End This event called, when a session expires.
Example:-
protected void Button1_Click(object sender, EventArgs e)
{
int count = 0;

if (Application["Visit"] != null)
{
count = Convert.ToInt32(Application["Visit"].ToString());
}

count = count + 1;
Application["Visit"] = count;
Label1.Text = "Total Visit = " + count.ToString();

You might also like