Home > DeveloperSection > Forums > How to use Session In Asp.net
Ankit Singh

Total Post:341

Points:2389
Posted on    January-17-2016 11:11 PM

 ASP.Net ASP.Net  Session 
Ratings:


 1 Reply(s)
 239  View(s)
Rate this:
I want to use Session In asp.net How to use this Please Help me.


aditya kumar Patel

Total Post:244

Points:1736
Posted on    January-17-2016 11:17 PM

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;
        }
    }
}

 


Don't want to miss updates? Please click the below button!

Follow MindStick