Session Using in Asp.Net C#

SESSION



The session state is used to maintain the session of each user throughout the application.

 Session allows information to be stored in one page and access in another page and support any type of object. 


The Session object stores information about, or change settings for a user session.

Variables stored in a Session object hold information about one single user, and are available to all pages in one application.

 Common information stored in session variables are name, id, and preferences.

 The server creates a new Session object for each new user, and destroys the Session object when the session expires.


DEMO




First  - Add New Web Form -  Select  Labels & Textbox & Buttton - Textbox Id Change Required Specfications









All Textbox  Change the  Id Values  for Required Specfications









                   Html Coding



<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
        <tr><td>
            <asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
            </td><td>
                <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
            </td></tr>
        <tr><td>
            <asp:Label ID="Label2" runat="server" Text="Age"></asp:Label>
            </td><td>
                <asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
            </td></tr>
        <tr><td>
            <asp:Label ID="Label3" runat="server" Text="Mobile"></asp:Label>
            </td><td>
                <asp:TextBox ID="txtMobile" runat="server"></asp:TextBox>
            </td></tr>
        <tr><td colspan="2">
            <asp:Button ID="Button1" runat="server" Text="Send" OnClick="Button1_Click" />
            </td></tr>

    </table>
    </div>
    </form>
</body>

</html>










Next - Add The New Web Form  -  Select  Labels  & All  Label Id Change For Session Passing Values








Html Coding


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>   

        <table>
             <tr><td colspan="2">  WELCOME TO HOMEPAGE</td></tr>
            <tr><td> <asp:Label ID="Label1" runat="server" Text="Name"></asp:Label></td>
             <td><asp:Label ID="lblName" runat="server" Text="Label"></asp:Label></td></tr>
              <tr><td> <asp:Label ID="Label2" runat="server" Text="Age"></asp:Label>
              </td><td> <asp:Label ID="lblAge" runat="server" Text="Label"></asp:Label></td></tr>

              <tr><td><asp:Label ID="Label3" runat="server" Text="Mobile"></asp:Label></td>
                  <td> <asp:Label ID="lblMobile" runat="server" Text="Label"></asp:Label></td></tr>          
           
        </table>    
     
        </div>
    </form>
</body>
</html>









Next - Double Click Send Button - Write the Below Coding


                                        Pass the  Values by using Session
                                             
                                    Session["Name"] = txtName.Text;

                         Session["Age"] = txtAge.Text;

                         Session["Mobile"] = txtMobile.Text;

                                                 

                                    Next Web   Form Name

                           Response.Redirect("homepage.aspx");


C# Coding



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

public partial class Sessio : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        txtName.Text = "DotNet";
        txtAge.Text = "29";
        txtMobile.Text = "0123456789";
    }
    protected void Button1_Click(object sender, EventArgs e)
    {


        Session["Name"] = txtName.Text;

        Session["Age"] = txtAge.Text;

        Session["Mobile"] = txtMobile.Text;


        Response.Redirect("homepage.aspx");

    }


}







Next  - Homepage Coding Page get the passing Session Values


              Session Values  Get  Label  


                               lblAge.Text = Session["Age"].ToString();

              lblName.Text= Session["Name"].ToString();

              lblMobile.Text=Session["Mobile"].ToString();



                        C# Coding




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

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

        lblAge.Text = Session["Age"].ToString();

        lblName.Text = Session["Name"].ToString();

        lblMobile.Text = Session["Mobile"].ToString();


    }

}








Next - Click - F5 Run the Web Form   -  Send Button Click   Textbox Values Passing Another Page Using    Session 









Home Page - Get the  Session Vaues  








0 comments:

Post a Comment