HOW TO USE SESSION IN ASP.NET

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
 502  View(s)
Ratings:
Rate this:
I want to use Session In asp.net How to use this Please Help me.
  1. aditya kumar Patel

    Post:254

    Points:1810
    Re: How to use Session In Asp.net

    Sessions we can be used to store  data for the user specific like cookies.
    sessions will use cookies to store the data.
    Sessions we can used easily in ASP.NET with the Session object. We will re-use the cookie example,
    and use sessions instead. sessions will expire after a certain amount of minutes,as configured in the web.config file.

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Sesion.aspx.cs" Inherits="Forumasp.Sesion" %>

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title></title>    
    </head>

    <body runat="server" id="Body">
        <form id="form1" runat="server">
            <div>
                Change Body Color:
                <asp:DropDownList runat="server" ID="color" AutoPostBack="True" OnSelectedIndexChanged="color_IndexChanged" Width="200">
                    <asp:ListItem Value="black" Selected="True">black</asp:ListItem>
                    <asp:ListItem Value="Blue">Blue</asp:ListItem>
                    <asp:ListItem Value="Red">Red</asp:ListItem>
                    <asp:ListItem Value="green">green</asp:ListItem>
                </asp:DropDownList>
            </div>
        </form>
    </body>
    </html>

    CodeBehind

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    namespace Forumasp
    {
        public partial class Sesion : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (Session["BackgroundColor"] != null)
                {
                    color.SelectedValue = Session["BackgroundColor"].ToString();
                    Body.Style["background-color"] = color.SelectedValue;
                }
            }

            protected void color_IndexChanged(object sender, EventArgs e)
            {
                Body.Style["background-color"] = color.SelectedValue;
                Session["BackgroundColor"] = color.SelectedValue;
            }
        }
    }

     

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!