Coming to usage you can use both cookies and session in the same page.
We should go for cookies to store something that we want to know when the user returns to the web page in future (eg. remember me on this computer check box on login pages uses cookies to remember the user when he returns). Sessions should be used to remember something for that particular browser session (like the user name, to display on every page or where ever needed).
how to write and read cookies.
This eample code belongs to web site www.gotdotnet.com visit the following link for full example code and demo.
http://samples.gotdotnet.com/quickstart/aspplus/doc/stateoverview.aspx
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Request.Cookies("preferences1") = Null Then
Dim cookie As New HttpCookie("preferences1")
cookie.Values.Add("ForeColor", "black")
...
Response.AppendCookie(cookie)
End If
End Sub
Protected Function GetStyle(key As String) As String
Dim cookie As HttpCookie = Request.Cookies("preferences1")
If cookie <> Null Then
Select Case key
Case "ForeColor"
Return(cookie.Values("ForeColor"))
Case ...
End Select
End If
Return("")
End Function
How to write and read session variables.
This example belongs to www.eggheadcafe.com visit the following link for quick summary and list of FAQs on Session State.
http://www.eggheadcafe.com/articles/20021016.asp
Basic use of Session in ASP.NET (C#):
STORE:
DataSet ds = GetDataSet(whatever parameters);
Session["mydataset")=ds;
RETRIEVE:
DataSet ds = (DataSet)Session["mydataset"];
0 comments:
Post a Comment