UncleCoder.com

UncleCoder.com

Free programming examples and instructions

C# List Or Object assign to ViewState - Asp.Net

Demo and code for how to assign C# object or C# List to ViewState in Asp.net

by Athil


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

Edited on 04 Jul 2019


Here I am going to show how to assign an Object or a List to a ViewState and take back from ViewState.

DEMO

Assigning object to ViewState

 User ur = new User();
 ur.Address = txtAddressSet.Text;
 ur.Name = TxtNameSet.Text;
 ur.Datetime = DateTime.Now;
 ViewState["UserDetails"] = ur;

Class Model

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

We must give [Serializable] On the top line of declaring a class that needs to assign in ViewState.

Taking Values from ViewState to object

 User ur = new User();
 ur = ViewState["UserDetails"] as User;

 

Full Demo Code

Aspx

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        Enter&nbsp; your name and address and press Set data to view state button<br />
        And Enter the Retrieve From View State button
        <br />
        <br />
        Name: <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="Set Data to view state" />
        </p>
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Retrieve From View state" />
        <br />
        Name: <asp:TextBox ID="txtNameGet" runat="server"></asp:TextBox>
        <br />
        Address:
        <asp:TextBox ID="TxtAddressGet" runat="server"></asp:TextBox>
    </form>


</body>

    
    

C#


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

        [Serializable]
        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.Address = txtAddressSet.Text;
            ur.Name = TxtNameSet.Text;
            ur.Datetime = DateTime.Now;

            ViewState["UserDetails"] = ur;

            lblMessage.Text = "Data Addedd to view state!!";
        }

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

        }

    }

 



Leave a Comment:


Advertisement