How to save data to ViewState in asp.net

Save data to ViewState
ViewStateDataStore.aspx

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        ViewState["Name"] = TextBox1.Text.ToString();
        Label1.Text = "Your name saved in ViewState.";
    }
    protected void Button2_Click(object sender, System.EventArgs e)
    {
        string name = (string)ViewState["Name"];
        
        Label1.Text = "Hi " + name + "! we found your name in ViewState.";
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>asp.net ViewState example: how to store (save) data in ViewState</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Red">asp.net ViewState example: data store</h2>
        <asp:Label 
            ID="Label1" 
            runat="server"
            Font-Size="Large"
            ForeColor="Teal"
            >
        </asp:Label>
        <br /><br />
        <asp:Label ID="Label2" runat="server" Text="Your Name" ForeColor="Green">
        </asp:Label>
        <asp:TextBox ID="TextBox1" runat="server" BackColor="Green" ForeColor="AliceBlue">
        </asp:TextBox>
        <br />
        <asp:Button 
            ID="Button1" 
            runat="server" 
            Text="Save data in ViewState"
            OnClick="Button1_Click"
            Font-Bold="true"
            ForeColor="DarkGreen"
            />
        <asp:Button 
            ID="Button2" 
            runat="server" 
            Text="Read data from ViewState"
            OnClick="Button2_Click"
            Font-Bold="true"
            ForeColor="DarkGreen"
            />
    </div>
    </form>
</body>
</html>
More asp.net examples