Hi,
I need one example session and cookies can you give me and
explain it.
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Satyapriya NayakRank #47 · 31.5K reputationPosted Jun 13, 2012, 1:33 AM EDT
Sessions can be used to store even complex data for the user just like cookies. Actually, sessions will use cookies to store the data, unless you explicitly tell it not to. Sessions can be used easily in ASP.NET with the Session object. We will re-use the cookie example, and use sessions instead. Keep in mind though, that sessions will expire after a certain amount of minutes, as configured in the web.config file. Markup code:
@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> html xmlns="http://www.w3.org/1999/xhtml" > head runat="server"> title>Sessionstitle> head> body runat="server" id="BodyTag"> form id="form1" runat="server"> asp:DropDownList runat="server" id="ColorSelector" autopostback="true" onselectedindexchanged="ColorSelector_IndexChanged"> asp:ListItem value="White" selected="True">Select color...asp:ListItem> asp:ListItem value="Red">Redasp:ListItem> asp:ListItem value="Green">Greenasp:ListItem> asp:ListItem value="Blue">Blueasp:ListItem> asp:DropDownList> form> body> html>using System; using System.Data; using System.Web; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if(Session["BackgroundColor"] != null) { ColorSelector.SelectedValue = Session["BackgroundColor"].ToString(); BodyTag.Style["background-color"] = ColorSelector.SelectedValue; } } protected void ColorSelector_IndexChanged(object sender, EventArgs e) { BodyTag.Style["background-color"] = ColorSelector.SelectedValue; Session["BackgroundColor"] = ColorSelector.SelectedValue; } }Cookies are small pieces of text, stored on the client's computer to be used only by the website setting the cookies. This allows webapplications to save information for the user, and then re-use it on each page if needed. Here is an example where we save a users choice of background color:
@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> html xmlns="http://www.w3.org/1999/xhtml" > head runat="server"> title>Cookiestitle> head> body runat="server" id="BodyTag"> form id="form1" runat="server"> asp:DropDownList runat="server" id="ColorSelector" autopostback="true" onselectedindexchanged="ColorSelector_IndexChanged"> asp:ListItem value="White" selected="True">Select color...asp:ListItem> asp:ListItem value="Red">Redasp:ListItem> asp:ListItem value="Green">Greenasp:ListItem> asp:ListItem value="Blue">Blueasp:ListItem> asp:DropDownList> form> body> html>using System; using System.Data; using System.Web; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if(Request.Cookies["BackgroundColor"] != null) { ColorSelector.SelectedValue = Request.Cookies["BackgroundColor"].Value; BodyTag.Style["background-color"] = ColorSelector.SelectedValue; } } protected void ColorSelector_IndexChanged(object sender, EventArgs e) { BodyTag.Style["background-color"] = ColorSelector.SelectedValue; HttpCookie cookie = new HttpCookie("BackgroundColor"); cookie.Value = ColorSelector.SelectedValue; cookie.Expires = DateTime.Now.AddHours(1); Response.SetCookie(cookie); } }http://asp.net-tutorials.com/state/sessions/
http://asp.net-tutorials.com/state/cookies/
Thanks