UncleCoder.com

UncleCoder.com

Free programming examples and instructions

C# List or Object to session -Asp.net

Demo and code for how to assignin C# list or Object tot session

by Athil


Posted on 27 Jul 2018 Category: Asp.net Views: 44

Edited on 10 Jul 2019


Here I am going to show how to assign C# List or object to JSON string.

DEMO

Data Model

        public class User
        {
            public string Name { get; set; }
            public string Address { get; set; }
            public DateTime Datetime { get; set; }
        }

Object to Session

        protected void Button1_Click(object sender, EventArgs e)
        {
            User ur = new User();
            ur.Name = txtNameSet.Text;
            ur.Address = txtAddressSet.Text;
            ur.Datetime = DateTime.Now;
            Session["UserDetails"] = ur;
            lblMessage.Text = "Added to session";
        }

Session To Object

        protected void Button2_Click(object sender, EventArgs e)
        {
            User ur = new User();
            ur = Session["UserDetails"] as User;
            txtNameGet.Text = ur.Name;
            txtAddressget.Text = ur.Address;

        }

Full Demo Code

Full Aspx Code



<!DOCTYPE html>

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

        Enter name and Address and click on add to session button<br />
        <br />
        Name:&nbsp;&nbsp;&nbsp; <asp:TextBox ID="txtNameSet" runat="server"></asp:TextBox>
    </div>
        Address:
        <asp:TextBox ID="txtAddressSet" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Label ID="lblMessage" runat="server"></asp:Label>
        <p>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Add to Session" />
        </p>
        Name :<asp:TextBox ID="txtNameGet" runat="server"></asp:TextBox>
        <p>
            Address:
            <asp:TextBox ID="txtAddressget" runat="server"></asp:TextBox>
        </p>
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Retireve from Session" />
    </form>
</body>
    
 

</html>

C# Code

        public class User
        {
            public string Name { get; set; }
            public string Address { get; set; }
            public DateTime Datetime { get; set; }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            User ur = new User();
            ur.Name = txtNameSet.Text;
            ur.Address = txtAddressSet.Text;
            ur.Datetime = DateTime.Now;
            Session["UserDetails"] = ur;
            lblMessage.Text = "Added to session";
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            User ur = new User();
            ur = Session["UserDetails"] as User;
            txtNameGet.Text = ur.Name;
            txtAddressget.Text = ur.Address;

        }

 



Leave a Comment: