Pages

Tuesday, 23 April 2013

Write a program in C# for managing session in ASP.NET.

Write a program in C# for managing session in ASP.NET.

Program:

Session.aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="session.aspx.cs" Inherits="session" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e) {
        Session["Name"] = TextBox1.Text.ToString();
        Session["sizar"] = TextBox1.Text.ToString();
        Session["Color"] = TextBox2.Text.ToString();
        Label1.Text = "Sizar your Session is saved!";
    }
    </script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" ForeColor="Crimson">
        </asp:Label>
        <br /><br />
        <asp:Label ID="Label2" runat="server" Text="Name">
        </asp:Label>
        <asp:TextBox ID="TextBox1" runat="server">
        </asp:TextBox>
        <br />
        <asp:Label ID="Label3" runat="server" Text="Favorite Color">
        </asp:Label>
        <asp:TextBox ID="TextBox2" runat="server">
        </asp:TextBox>
        <br />
        <asp:Button  ID="Button1"  runat="server" Font-Bold="true" Text="Save Into Session" OnClick="Button1_Click" />
        <br /><br />
        <asp:HyperLink   ID="HyperLink1"   runat="server"    Text="Go for session test"  NavigateUrl="~/SessionTest.aspx">
        </asp:HyperLink>
    </div>
    </form>
</body>
</html>


session.aspx.cs

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

public partial class session : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

sessionTest.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SessionTest.aspx.cs" Inherits="SessionTest" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">  
    protected void Page_Load(object sender, System.EventArgs e) {
        label1.Text = "Hi " + Session["Name"];
        label1.Text += "Hi " + Session["sizar"];
        label1.Text += "!<br />Your favorite color is " + Session["Color"];  
    }  
</script> 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <div>  
        <asp:Label   ID="label1" runat="server"  Font-Size="Larger"  ForeColor="SeaGreen"  >  
        </asp:Label>  
        <br /><br />  
        <asp:HyperLink    ID="HyperLink1"  runat="server"   Text="Go session page"  NavigateUrl="~/Session.aspx">  
        </asp:HyperLink>              
       </div>  
    </div>
    </form>
</body>
</html>

sessionTest.aspx.cs

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

public partial class SessionTest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

Demo:

Write a program in C# for managing session in ASP.NET.
Write a program in C# for managing session in ASP.NET.


No comments:

Post a Comment