A cookie is a small piece of
text file stored on user's computer in the form of name-value pair. We use
cookies to small information on client’s computer. Common use of cookies is to
remember users between visits.
Types of Cookies:
1.
persist cookies:
It is also known as permanent
cookies. Persist cookies are store in hard disk until they expired or deleted.
These are not affected by the browser setting. We set the expiry time for
cookie.
2.
Non persist cookies (temporary cookies):
It is also known as session
cookies. It stored in memory of web browser, as we close the browser it will
lost.
Implementation:
Add a webform to project and
design the page as mention below:
<fieldset style="width:400px;height:auto;">
<legend><strong>Cookies
Example:</strong></legend>
<table>
<tr><td>Name:</td><td><asp:TextBox ID="txtname"
runat="server"></asp:TextBox></td></tr>
<tr><td></td><td><asp:Label ID="lblerror" runat="server"></asp:Label></td></tr>
<tr><td><asp:Button ID="btnsubmit" runat="server" Text="Submit" OnClick="btnsubmit_Click" /></td><td><asp:Button ID="btncheck" runat="server" Text="Read Cookies" OnClick="btncheck_Click" /></td></tr>
</table>
</fieldset>
IN C#:
protected void btnsubmit_Click(object sender, EventArgs e)
{
HttpCookie ck = new HttpCookie("cookie1");
ck["Cookiename"] = txtname.Text;
Response.Cookies.Add(ck);
lblerror.Text = "Cookies Created Successfully";
txtname.Text = string.Empty;
}
protected void btncheck_Click(object sender, EventArgs e)
{
lblerror.Text = "";
HttpCookie ck =
Request.Cookies["cookie1"];
string Cookiename;
if (ck != null)
{
bookname = ck["Cookiename"];
txtname.Text = Cookiename;
}
}
IN VB:
Protected Sub btnsubmit_Click(ByVal sender As Object, ByVal e AsSystem.EventArgs) Handles btnsubmit.Click
Dim ck As New HttpCookie("cookie1")
ck("Cookiename ") = txtname.Text
Response.Cookies.Add(ck)
lblerror.Text = "Cookies Created
Successfully"
txtname.Text = String.Empty
End Sub
Protected Sub btncheck_Click(ByVal sender As Object, ByVal e AsSystem.EventArgs) Handles btncheck.Click
lblerror.Text = ""
Dim ck As HttpCookie = Request.Cookies("cookie1")
Dim Cookiename As String
If ck IsNot Nothing Then
Cookiename = ck("Cookiename")
txtname.Text = Cookiename
End If
End Sub
Create persist cookie:
IN C#:
protected void btnsubmit_Click(object sender, EventArgs e)
{
HttpCookie ck = new HttpCookie("cookie1");
ck["cookiename"] = txtname.Text;
//cookie
expire in 1 day
ck.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(ck);
lblerror.Text = "Cookies Created Successfully";
txtname.Text = string.Empty;
}
In VB:
Protected Sub btnsubmit_Click(ByVal sender As Object, ByVal e AsSystem.EventArgs) Handles btnsubmit.Click
Dim ck As New HttpCookie("cookie1")
ck("cookiename") = txtname.Text
ck.Expires = DateTime.Now.AddDays(1)
Response.Cookies.Add(ck)
lblerror.Text = "Cookies Created
Successfully"
txtname.Text = String.Empty
End Sub
How remove persistent cookies before expiration
time:
IN C#:
protected void btnsubmit_Click(object sender, EventArgs e)
{
HttpCookie ck = new HttpCookie("cookie1");
ck["cookiename"] = txtname.Text;
//Expire the
cookie before expiration time
ck.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(ck);
lblerror.Text = "Cookies Created Successfully";
txtname.Text = string.Empty;
}
IN VB:
Protected Sub btnsubmit_Click(ByVal sender As Object, ByVal e AsSystem.EventArgs) Handles btnsubmit.Click
Dim ck As New HttpCookie("cookie1")
ck("cookie1name") = txtname.Text
'Expire the cookie before
expiration time
ck.Expires = DateTime.Now.AddDays(-1)
Response.Cookies.Add(ck)
lblerror.Text = "Cookies Created
Successfully"
txtname.Text = String.Empty
End Sub
0 comments:
Post a Comment