Monday, June 26, 2017

Setting and Retrieving Cookies in ASP .net

A simple yet good example for explaining how we can set and retrieve cookies through an ASP .net web page. Cookies are small bit of text, which can be stored either in client (Web Browser) or on Server.
Partial Class Cookies
Inherits System.Web.UI.Page

Protected Sub btnSet_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSet.Click
 Dim cookieExample As New HttpCookie(CookieExample.txt”)
 cookieExample.Item(“name”) = txtName.Text
 cookieExample.Item(“class”) = txtClass.Text
 cookieExample.Expires = Date.Now.AddSeconds(30)
 Response.Cookies.Add(cookieExample) ‘Adding cookie to local machine
End Sub

Protected Sub btnRetrieve_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRetrieve.Click
  txtName2.Text = Request.Cookies(CookieExample.txt”).Item(“name”)
  txtClass2.Text = Request.Cookies(CookieExample.txt”).Item(“class”)
  Catch ex As Exception
  MsgBox(“Sorry, session expired..!!”)
 End Try
End Sub

End Class

This Is The Newest Post