Thursday, February 18, 2010

State (Cookies, sessions etc.)

Here is the code on the form side (Where you enter the data)with all three different methods


Partial Class Default2
Inherits System.Web.UI.Page

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
' QueryString()
'SessionMethod()
CookieMethod()
End Sub

Protected Sub QueryString()
Response.Redirect("Default3.aspx?Firstname=" & txtFirstName.Text & "&Lastname=" & txtLastName.Text)
End Sub

Protected Sub SessionMethod()
Session("First") = txtFirstName.Text
Session("Last") = txtLastName.Text
Response.Redirect("Default3.aspx")
End Sub

Protected Sub CookieMethod()
Dim chocolateChip As New HttpCookie("sugar")
chocolateChip("first") = txtFirstName.Text
chocolateChip("last") = txtLastName.Text
chocolateChip.Path = "C:\Sugar.txt"
Response.Cookies.Add(chocolateChip)

Response.Redirect("Default3.aspx")
End Sub
End Class

Here is the code from the receiving side:


Partial Class Default3
Inherits System.Web.UI.Page


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'GetQueryString()
'GetSession()
ReadCookie()
End Sub

Protected Sub GetQueryString()
lblFirstName.Text = Request.QueryString("FirstName")
lblLastName.Text = Request.QueryString("LastName")
End Sub

Protected Sub GetSession()
If Session("First") <> Nothing Then
lblFirstName.Text = Session("First")
lblLastName.Text = Session("Last")
End If
End Sub

Protected Sub ReadCookie()
Dim cookie As HttpCookie = Request.Cookies("C:\sugar.txt")
lblFirstName.Text = cookie("first")
lblLastName.Text = cookie("last")
End Sub
End Class

No comments:

Post a Comment