Introduction:
Here I will explain how to create and read cookie values in asp.net using c#, vb.net with example or write and get cookie values in asp.net using c#, vb.net with example. By using Response.Cookies and Request.Cookies properties we can create and read or get cookie values in asp.net using c#, vb.net.
Description:
In previous articles I explained Asp.net mvc project example with demo, Bind specific columns of datatable to datagridview in c#, bind gridview using jquery json method in asp.net, show multiple markers in google map from database in asp.net and many articles relating to gridview, asp.net, c#,vb.net and jQuery. Now I will explain how to create, read or get or write cookie values in asp.net using c#, vb.net with example.
In previous articles I explained Asp.net mvc project example with demo, Bind specific columns of datatable to datagridview in c#, bind gridview using jquery json method in asp.net, show multiple markers in google map from database in asp.net and many articles relating to gridview, asp.net, c#,vb.net and jQuery. Now I will explain how to create, read or get or write cookie values in asp.net using c#, vb.net with example.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Create, Write and Read
Cookies in Asp.net using C#, VB.NET</title>
</head>
<body>
<form id="form1"
runat="server">
<div>
<asp:Button ID="btnCreate"
runat="server"
Text="Create
Cookies" onclick="btnCreate_Click" />
<asp:Button ID="btnGet"
runat="server"
Text ="Get
Cookie Values" onclick="btnGet_Click" /><br /><br />
<asp:Label ID="lblResult"
runat="server"/>
</div>
</form>
</body>
</html>
|
After completion of aspx page add following namespaces in
codebehind
C#
Code
using System;
using System.Web;
|
After completion of adding namespaces you need to write the
code like as shown below
protected void btnCreate_Click(object
sender, EventArgs e)
{
Response.Cookies["UserId"].Value
= "1234";
Response.Cookies["UserId"].Expires
= DateTime.Now.AddDays(1);
Response.Cookies["UserName"].Value
= "Suresh";
Response.Cookies["UserName"].Expires
= DateTime.Now.AddDays(1);
lblResult.Text = "Cookie Values Created
Successfully";
}
protected void btnGet_Click(object
sender, EventArgs e)
{
string userid =
Request.Cookies["UserId"].Value;
string username =
Request.Cookies["UserName"].Value;
lblResult.Text = "<b>Cookie
Values</b> : UserId: " + userid + ",
UserName: " + username;
}
|
VB.NET
Code
Imports System.Web
Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As
Object, e As EventArgs)
End Sub
Protected Sub btnCreate_Click(ByVal
sender As Object,
ByVal e As EventArgs)
Response.Cookies("UserId").Value
= "1234"
Response.Cookies("UserId").Expires
= DateTime.Now.AddDays(1)
Response.Cookies("UserName").Value
= "Suresh"
Response.Cookies("UserName").Expires
= DateTime.Now.AddDays(1)
lblResult.Text = "Cookie Values
Created Successfully"
End Sub
Protected Sub btnGet_Click(ByVal
sender As Object,
ByVal e As EventArgs)
Dim userid As String =
Request.Cookies("UserId").Value
Dim username As String =
Request.Cookies("UserName").Value
lblResult.Text = Convert.ToString((Convert.ToString("<b>Cookie
Values</b> : UserId: ") & userid) + ", UserName: ") & username
End Sub
End Class
|
Demo
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
1 comments :
Good one
Note: Only a member of this blog may post a comment.