STATE
MANAGEMENT TECHNIQUE
JUST SEE BELOW THE RESULT. IT WILL TELL YOU
WHAT THE NEED TO MANAGE THE OBJECT
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
public partial
class _Default
: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs
e)
{
}
List<string>
li = new List<string>();
protected void
Button1_Click(object sender, EventArgs e)
{
li.Add("ducat");
li.Add("noida");
}
protected void
Button2_Click(object sender, EventArgs e)
{
Response.Write(li[0]);
}
}
HERE IT SHOW NOTHING IN REQUEST
BECAUSE AN EACH AND EVERY REQUEST IT CREATE A NEW OBJECT IN FIRST CLICK IT ADD
ITEMS IN LIST WITH A NEW OBJECT BUT WHEN WE CLICK ON SECOND BUTTON TO SEE THE
RECORD IT CREATE A NEW OBJECT THAT’S WHY WE HAVE TO MANAGE THIS STATE BETWEEN
THE THESE TWO REQUEST
STATE MANAGEMENT
TECHNIQUE
SERVER SIDE STATE
MANAGEMENT CLIENT SIDE STATE
MANAGEMENT
CLIENT
SIDE OBJECT
1.
COOKIE COOKIE IS AN SMALL TEXT FILE IN WHICH DATA
CAN BE STORE MAXIMUM OF 4 KB IN WHICH FIFTY CHARACTERS ARE OF SERVER IN A PLAIN
TEXT FORMAT. WE CAN READ THESE FILES
·
COOKIES ARE
ACCESS AT SERVER SIDE BY THE REQUEST OBJECT
·
WE SEND TO THE
CLIENT BY ADDING THE OBJECT WITH RESPONSE TO THE CLIENT
·
COOKIE ARE NOT
RELIABLE
·
CLINET CAN DO
CHANGES ON IT
·
WE SHOULD NOT
STORE SENSITIVE FILES IN COOKIE BECAUSE USER CAN DELETE OR CAN DO CHANGES ON IT
·
COOKIE ARE NOT
SECURED
·
COOKIE IS NAME
BASED, SO WE MUST GIVE NAME TO THE
IDENTIFY THAT COOKIE
int i = 1;
protected void Button1_Click(object
sender, EventArgs e)
{
i++;
Response.Write(i);
}
·
HTTP COOKIE IS AN
CLASS IN WHICH WE CREATE OUR COOKIE
HTTPCOOKIE CK=NEW HTTPCOOKIE ();
CK. VALUES=” ”;
RESPONSE.COOKIES.ADD
(CK);
·
COOKIES ARE NOT
PAGE SPECIFIC WE CAN CALL THEM AND CAN USE THEM ON ANY PAGE NOT EVEN ON THAT
PAGE IN WHICH IT CREATED
TYPE OF COOKIE
STORED IN HARDDISK STORED IN
BROWSER PROCESS
PERMANANENT COOKIE
PRESISTENT COOKIE NON PRESISTENT COOKIE
|
|
|
|
|
WE CAN ACCESS IT IN ANY
WHERE IN ANY BROWSER OR IN ANY PAGE
|
|
|
WE CAN ACCESS THEM IN A
SINGLE BROWSER
|
|
` TO MAKE PRESISTENT OR NON-PRESISTENT COOKIE
WHEN WE GIVE THE EXPIRE TIME LIMIT IS CALL THEN PRESISTEN COOKIE AND
STORED IT IN PERMANENT MEMORY AND
ck.expires=datetime.now.addhours(60);
AND WHEN WE DONOT GIVE THE EXPIRE LIMIT IS
CALLED NON PRESISTENT COOKIE AND
STORED IN TEMPORARY MEMORY IN BROWSER PROCESS
· COOKIES ARE MANAGED TROUGH SERVER AND BROWSER
· WE CAN GIVE THE NUMBER OF LIMITS OF COOKIE IN A
BROWSER OR FOR MACHINE
· IN COOKIES DATA STORED IN KEY PER VALUE
COOKIES ARE TWO VALUE TYPE
SINGLE
KEY VALUE MULTI KEY
VALUE
COOKIE COOKIE
TO ACCESS THE VALUE , NAME,
TO UPDATE, TO CHANGE SOMETHING IN ALLREADY CREATE COOKIE BY ITS OBJECT
protected void Button1_Click(object sender, EventArgs
e)
{
HttpCookie
ck = Request.Cookies["my cookie"];
Response.Write(Request.Cookies["my cookie"].Value);
Response.Write(Request.Cookies["mycookie"]["first"].value);
Response.Write(Request.Cookies["my cookie"]["first"].Name.toString());
}
·
TO CHECK THAT
COOKIE HAS KEY OR NOT OF SINGLE VALUE OR MULTIPLE VALUE COOKIE
ck.HasKeys
TO UPDATE A COOKIE
· MAKE A NEW COOKIE WITH SAME NAME
· CALL THE OBJECT OF PREVIOUS COOKIE DO CHANGES, AND RESPONSE IT WITH SAME NAME
protected void Button1_Click(object
sender, EventArgs e)
{
HttpCookie
ck = Request.Cookies["my cookie"];
ck.Value = "ducat";
ck.Expires = DateTime.Now.AddMinutes(4);
Response.Cookies.Add(ck);
}
To DELETE COOKIE
CREATE A NEW COOKIE WITH SAME NAME WITH THE
EXPIRATION TIME IN MINUS ( - ) MINUTES.
protected void Button1_Click(object
sender, EventArgs e)
{
HttpCookie
ck = new HttpCookie["MYSITE"];
ck.Value = "ducat";
ck.Expires = DateTime.Now.AddMinutes(-5);
Response.Cookies.Add(ck);
}
NON PRESISTENT COOKIE WHICH RUN UNDER BROWSER
PROCESS
CREATE COOKIE
protected void Button1_Click(object sender, EventArgs
e)
{
HttpCookie
ck = new HttpCookie("mycookie");
ck.Value = "ducat";
Response.Cookies.Add(ck);
Response.Redirect("Default3.aspx");
}
ON SECOND FORM3 TO ACCESS THIS
VALUE OF YOUR COOKIE
protected void Page_Load(object sender, EventArgs
e)
{
string
s = Request.Cookies["mycookie"].Value;
Response.Write(s);
}
· IN COOKIE WE STORE AND ACCESS THE DATA IN STRING FORMAT ONLY
NOTE: TO MOVE ALL CONTROLS FREELY IN PAGE
CLICK ON TOOLS CLICK ON OPTION CLICK ON
HTML DESIGN CLICK ON CSS STYLING CLICK ON LAST (CHECK THE CHECK
BOX) OF CHANGE POSITIONIG TO ABSOLUTE
·
BUT THIS IS NOT
APPLY FOR THE LABEL SO WE HAVE WRITE ON SOURCE CODE OF PAGE ( ADD THIS IN CODE OF LABEL DESIGNING)
<STYLE= “POSITION: ABSOLUTE”>
TO CHECK THE COOKIE IS AVAILABEL OR NOT
protected void read_Click(object
sender, EventArgs e)
{
if
(Request.Cookies["mycookie"] != null)
{
string
s = Request.Cookies["mycookie"].Value;
Response.Write(s);
}
}
TO MAKE PRESISTENT COOKIE
CREATE
COOKIE
protected void Button1_Click(object
sender, EventArgs e)
{
HttpCookie
ck = new HttpCookie("mycookie");
//ck.Value =
"ducat";
ck.Value = TextBox1.Text;
ck.Expires = DateTime.Now.AddSeconds(38);
Response.Cookies.Add(ck);
Response.Redirect("Default2.aspx");
}
TO UPDATE
r
protected void UPDATE_Click(object
sender, EventArgs e)
{
HttpCookie
ck = Request.Cookies["mycookie"];
ck.Value = TextBox1.Text;
ck.Expires = DateTime.Now.AddSeconds(100);
Response.Cookies.Add(ck);
}
TO DELETE
protected void Button1_Click(object sender, EventArgs
e)
{
HttpCookie
ck = Request.Cookies["mycookie"];
ck.Expires = DateTime.Now.AddSeconds(-100);
Response.Cookies.Add(ck);
}
CREATE MULTIPLE KEY VALUE COOKIE
protected void MULTIPLE_Click(object sender, EventArgs
e)
{
HttpCookie
ck = new HttpCookie("mycookie");
//ck.Value =
"ducat";
ck.Values["fname"] =
TextBox1.Text;
ck.Values["lname"]
= TextBox2.Text;
ck.Expires = DateTime.Now.AddSeconds(20);
Response.Cookies.Add(ck);
Response.Redirect("Default2.aspx");
}
TO REMOVE
ANY SUBKEY VALUE
protected void MULTIPLE_Click(object sender, EventArgs
e)
{
HttpCookie
ck = Request.Cookies["mycookie"];
ck.Values.Remove["fname"];
}
TO READ ALL COOKIES
FIRSTLY CREATE
SINGLE VALUE COOKIE
protected void Button1_Click(object
sender, EventArgs e)
{
HttpCookie
ck = new HttpCookie("mycookie");
//ck.Value =
"ducat";
ck.Value = TextBox1.Text;
ck.Expires = DateTime.Now.AddSeconds(38);
Response.Cookies.Add(ck);
}
THAN CREATE MULTI KEY VALUE
protected void MULTIPLE_Click(object sender, EventArgs
e)
{
HttpCookie
ck = new HttpCookie("USERINFO");
//ck.Value =
"ducat";
ck.Values["fname"]
= TextBox1.Text;
ck.Values["lname"]
= TextBox2.Text;
ck.Expires = DateTime.Now.AddSeconds(20);
Response.Cookies.Add(ck);
Response.Redirect("Default2.aspx");
}
READ_CLICK
For(int
i=0;i<request.cookies.count;i++)
{
stringBuilder sb=new
StringBuilder();
httpcookie
ck=request.cookie[i];
sb.append(“cookie name =” +
ck.name);
sb.appendline();
if(ck.haskey)
{
Namevaluecollection
values=ck.values;
String []
subkey=ck.values.allkeys;
For(int
j=0;j<values.count;j++)
{
Sb.append(“subkey” +
subkey[j]);
Sb.append(values[j]);
Sb.appendline(“<br/>”);
}
}
Else
{
Sb.append(ck.values =”
+ck.vlaues);
Sb.appendlline(“<br/>”);
}
}
Response.write(sb.tostring());
}
To USE STRING BUILDER ADD NAME SPACE
USING SYSTEM.TEXT;
TO USED NAME VALUE
COLLECTION ADD NAME SPACE
USING SYSTEM.COLLECTION.SPECILIZED;
@ WHY WE USE STRING BUILDER
ANS. WHEN WE DOING
ADDING CONTINUOS ITEMS IN IT, IT ALWAYS
CREATE A NEW OBJECT FOR THAT NEW VALUE. SO TO ESCAPE THIS WE USE STRING BUILDER
NOTE
CK . HTTPONLY(
FALSE)
BY DEFAULT IT IS TRUE
IF WE WANT TO ACCESS ALL COOKIE TO
BE ACCESSED ONLY AT SERVER SIDE NOT BY THE CLIENT SIDE DO IT
CK . SECURE ( TRUE) DO IT TRUE
SSL(SECURE SOCKET LAYER)
IF
WE WANT TO SECURE OUR COOKIE TO MAKE IT UNACCESSIBLE BY ANOTHER NETWORK WHEN WE
ARE TRANSFERRING IT ON NETWORK.
REQUEST . BROWSER .
COOKIE
DO IT TRUE TO MAKE YOUR COOKIE SUPPORTABLE BY THE BROWSER
TO BLOCK ALL COOKIES AT CLIENT SIDE
OPEN BROWSER THAN
CLICK ON TOOL THAN CLICK ON INTERNET
OPTION THAN CLICK ON PRIVACY THAN
CLICK ON BLOCK ALL COOIKIES