state management in asp client side
TRANSCRIPT
-
7/29/2019 State Management in ASP Client Side .
1/27
State Management In Asp.Net
Client Side
-
7/29/2019 State Management in ASP Client Side .
2/27
Introduction
State Management is the process by which we
maintain session related information and
additional information about the controls and
its state.
The necessity of state management arises
when multiple users request for the same or
different Web Pages of a Web Site.
-
7/29/2019 State Management in ASP Client Side .
3/27
Types Of State Management
There are two types of state management:
1. Server Side
Data is stored in memory on the server. Itincludes application state, session state andprofiles.
2. Client Side
Data is stored on the client in various ways. Itincludes cookies, viewstate, query strings andhidden fields.
-
7/29/2019 State Management in ASP Client Side .
4/27
Types to be covered:
1. Cookies
2. Query Strings.
3. Hidden Field.
4. ViewState 5. Applications
6. Sessions
7. Profiles
-
7/29/2019 State Management in ASP Client Side .
5/27
Cookies
A cookie is a small text file sent by web serverand saved by web browser on client machine.
Cookies are also known by many names, HTTP
Cookie, Web Cookie, Browser Cookie, SessionCookie, etc.
Common use of cookies is to remember users
between visits. In each cookie you can not save more than 4k
of information.
-
7/29/2019 State Management in ASP Client Side .
6/27
clients can open cookies and see the contentof a cookie so they are not secure. Cookies are
usually used to save user preferenceinformation.
Size of cookies is limited to 4096 bytes.
Total 20 cookies can be used on a singlewebsite; if you exceed this browser will deleteolder cookies.
End user can stop accepting cookies by
browsers, so it is recommended to check theusers state and prompt the user to enable
cookies.
-
7/29/2019 State Management in ASP Client Side .
7/27
Types OF Cookies
There are 2 different types of cookies:
1. PERSISTANT COOKIES:
A persistent cookie is one stored as a file on your
computer, and it remains there when you closeInternet Explorer.
2. TEMPORARY(SESSION) COOKIES:
A temporary or session cookie is stored only for
your current browsing session, and is deletedfrom your computer when you close yourbrowser.
-
7/29/2019 State Management in ASP Client Side .
8/27
Example 1:
Creating Cookies(Using HttpCookie Class):
Take a new website and make design page as
shown in the next slide.
Do coding on login button click as shown
below:
-
7/29/2019 State Management in ASP Client Side .
9/27
-
7/29/2019 State Management in ASP Client Side .
10/27
Coding on login button click:
protected void Button1_Click(object sender,
EventArgs e){
HttpCookie mycookie = newHttpCookie("cookie1");
mycookie["Name"] = TextBox1.Text;mycookie["password"] = TextBox2.Text;
Response.Cookies.Add(mycookie);
mycookie.Expires = DateTime.Now.AddDays(1);
Response.Redirect("home.aspx");
}
-
7/29/2019 State Management in ASP Client Side .
11/27
Make another page as home.aspx and do codingon page load as shown:
protected void Page_Load(object sender,
EventArgs e) {
HttpCookie reqcookie =Request.Cookies["cookie1"];
if (reqcookie != null)
{
Label1.Text = reqcookie["Name"];
Label2.Text = reqcookie["Password"]; }
}
-
7/29/2019 State Management in ASP Client Side .
12/27
Example:
Create Cookies( Using Response)
Take a textbox and a button.
On button click, do coking as:
protected void Button1_Click(object sender,EventArgs e)
{
Response.Cookies["StudentCookies"].Value =TextBox1.Text;
Response.Cookies["StudentCookies"].Expires
= DateTime.Now.AddDays(1); Response.Redirect("home.aspx");
}
-
7/29/2019 State Management in ASP Client Side .
13/27
On home page, on page load, do coding as:
TextBox1.Text =
Request.Cookies["StudentCookies"].Value;
-
7/29/2019 State Management in ASP Client Side .
14/27
Using Multi Valued Cookies
You can also store multiple name-value pairs
in a single cookie.
The name-value pairs are referred to as
subkeys.
A cookie with subkeys also helps you limit the
size of cookie files.
-
7/29/2019 State Management in ASP Client Side .
15/27
Example 3:
Take a button and do coding as follows:
protected void Button1_Click(object sender, EventArgs e)
{ Response.Cookies["StudentCookies"]["RollNumber"] = "12";
Response.Cookies["StudentCookies"]["FirstName"] ="Abhimanyu";
Response.Cookies["StudentCookies"]["MiddleName"] =
"Kumar"; Response.Cookies["StudentCookies"]["LastName"] = "Vatsa";
Response.Cookies["StudentCookies"]["TotalMarks"] = "499";
Response.Cookies["StudentCookies"].Expires =DateTime.Now.AddDays(1);
Response.Redirect("home2.aspx"); }
-
7/29/2019 State Management in ASP Client Side .
16/27
Take another page as home2.aspx and do coding on page load as:
protected void Page_Load(object sender, EventArgs e)
{
string roll; roll = Request.Cookies["StudentCookies"]["RollNumber"];
roll = roll + " " +Request.Cookies["StudentCookies"]["FirstName"];
roll = roll + " " +
Request.Cookies["StudentCookies"]["MiddleName"]; roll = roll + " " +
Request.Cookies["StudentCookies"]["LastName"];
roll = roll + " " +Request.Cookies["StudentCookies"]["TotalMarks"];
Label1.Text = roll; }
-
7/29/2019 State Management in ASP Client Side .
17/27
Query Strings In Asp.Net
A query string is information that is appendedto the end of a page URL.
Example:
http://www.contoso.com/listwidgets.aspx?category=basic&price=100
In the URL path above, the query string starts
with a question mark (?) and includes twoattribute/value pairs, one called "category"and the other called "price."
-
7/29/2019 State Management in ASP Client Side .
18/27
Query strings provide a simple but limited way
to maintain state information.
some browsers and client devices impose a2083-character limit on the length of the URL.
-
7/29/2019 State Management in ASP Client Side .
19/27
Example:
protected void Button1_Click(object sender,
EventArgs e) {
int recordid = 10;
Response.Redirect("Default2.aspx?recordid=" +recordid.ToString());
}
Get the value in the same or another page. string id= Request.QueryString["recordid"];
-
7/29/2019 State Management in ASP Client Side .
20/27
Hidden Feilds
A HiddenField control stores a single variable in itsValue property and must be explicitly added to thepage.
The information in a HiddenField control is not dis
it can be read and set in client script. played when thebrowser renders the page.
Example:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hiddenfield(v=vs.90).aspxhttp://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hiddenfield.value(v=vs.90).aspxhttp://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hiddenfield.aspxhttp://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hiddenfield.aspxhttp://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hiddenfield.value(v=vs.90).aspxhttp://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hiddenfield(v=vs.90).aspx -
7/29/2019 State Management in ASP Client Side .
21/27
ViewState Controls
The ViewState property provides a dictionary
object for retaining values between multiple
requests for the same page.
When the page is processed, the current state ofthe page and controls is hashed into a string and
saved in the page as a hidden field, or multiple
hidden fields if the amount of data stored in theViewState property exceeds the specified value in
the MaxPageStateFieldLength property.
http://msdn.microsoft.com/en-us/library/system.web.ui.control.viewstate(v=vs.90).aspxhttp://msdn.microsoft.com/en-us/library/system.web.ui.control.viewstate(v=vs.90).aspxhttp://msdn.microsoft.com/en-us/library/system.web.ui.page.maxpagestatefieldlength(v=vs.90).aspxhttp://msdn.microsoft.com/en-us/library/system.web.ui.page.maxpagestatefieldlength(v=vs.90).aspxhttp://msdn.microsoft.com/en-us/library/system.web.ui.control.viewstate(v=vs.90).aspxhttp://msdn.microsoft.com/en-us/library/system.web.ui.control.viewstate(v=vs.90).aspx -
7/29/2019 State Management in ASP Client Side .
22/27
Make a login application in which user is given 3 chances tologin:
Code:
int counter = 3; SqlConnection con = new SqlConnection();
protected void Page_Load(object sender, EventArgs e)
{
con.ConnectionString =ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
if (con.State == ConnectionState.Closed)
{
con.Open();
}
-
7/29/2019 State Management in ASP Client Side .
23/27
if (!Page.IsPostBack)
{
Label2.Text = counter.ToString();
ViewState["count"] = counter; }
protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select password from Table1where name=@sname";
cmd.Connection = con;
cmd.Parameters.Add("@sname",SqlDbType.VarChar,50).Value = TextBox1.Text;
-
7/29/2019 State Management in ASP Client Side .
24/27
SqlDataReader dr1 = cmd.ExecuteReader();
if (dr1.HasRows)
{
dr1.Read();
if (TextBox2.Text == dr1["password"].ToString())
{
Label1.Text = "you are successfully login";
}
else
{ int i = (int)ViewState["count"];
i = i - 1;
ViewState["count"] = i;
-
7/29/2019 State Management in ASP Client Side .
25/27
if (i > 0)
{
Label1.Text = "retry ...you have " + i +
"chances left now";
}
else
{ Response.Redirect("user.aspx");
}
}
}
dr1.Close();
cmd.Dispose();
//dr.Close();
-
7/29/2019 State Management in ASP Client Side .
26/27
Comparison
1. ViewState can be applied to all data types
of asp.net
QueryStrings are limited to string data type.
Cookies are limited to string types.
2. ViewState values are stored in hidden fields.
Querystring in browsers url. Cookies in clients computer.
-
7/29/2019 State Management in ASP Client Side .
27/27
3. ViewState lifetime is permanent.
Querystrings are lost when the user enters a
new url or closes the browser.
Cookies lifetime are set by programmers.
4. Scope of viewstate is limited to current
page.
Scope of querystrings are is limited to the
target page.
Scope of the cookies are limited to the entire
application.