Cookies in ASP.NET using VB.NET
In this article we will learn how to create cookies in ASP.NET.
In this article you will learn how to create cookies in ASP.NET.
Request object is used to read the data on client using Cookies collection.
Response object's Cookies collection is used to send the cookie to the client.
Use HttpCookie class to create the cookies.
Cookies can be of two types.
Temporary Cookie (default)
To make a cookie as permanent cookie use Expires property of a cookie
Create a web form having two buttons.
When Create Cookies is click then manage a cookie as lv to manage the last visit.
Form looks like this.
Creating the Cookie: To create cookies add the following code with create cookie button.
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim c As New HttpCookie("lv")
c.Value = DateTime.Now.ToString()
c.Expires = DateTime.Now.AddMonths(2)
Reading the value of cookie: To Reading the value of cookie add the following code with show cookie button.
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
If Request.Cookies("lv") IsNot Nothing Then
Response.Write("Cookie not found")
Now save and run the application.
Now we click on the button create cookies cookies will be created. and to show the value of cookies click on the button show cookies.