How to read data from ViewState in asp.net

Read data from ViewState
ViewStateDataRead.aspx

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

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        ViewState["FavoriteColor"] = TextBox1.Text.ToString();
        ViewState["City"] = TextBox2.Text.ToString();
        Label1.Text = "Your data saved in ViewState.";
    }
    protected void Button2_Click(object sender, System.EventArgs e)
    {
        string color = (string)ViewState["FavoriteColor"];
        string city = (string)ViewState["City"];

        Label1.Text = "Hi your favorite color is: " + color +
                "<br />and you came from: " + city;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>asp.net ViewState example: how to read data from ViewState</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Navy">asp.net ViewState example: data read</h2>
        <asp:Label 
            ID="Label1" 
            runat="server"
            Font-Size="Large"
            ForeColor="SeaGreen"
            >
        </asp:Label>
        <br /><br />
        <asp:Label 
            ID="Label2" 
            runat="server" 
            Text="Favorite Color" 
            ForeColor="HotPink"
            >
        </asp:Label>
        <asp:TextBox 
            ID="TextBox1" 
            runat="server" 
            BackColor="HotPink" 
            ForeColor="Snow"
            >
        </asp:TextBox>
        <br />
        <asp:Label 
            ID="Label3" 
            runat="server" 
            Text="City" 
            ForeColor="HotPink"
            >
        </asp:Label>
        <asp:TextBox 
            ID="TextBox2" 
            runat="server" 
            BackColor="HotPink" 
            ForeColor="Snow"
            >
        </asp:TextBox>
        <br />
        <asp:Button 
            ID="Button1" 
            runat="server" 
            Text="Save data in ViewState"
            OnClick="Button1_Click"
            Font-Bold="true"
            ForeColor="HotPink"
            />
        <asp:Button 
            ID="Button2" 
            runat="server" 
            Text="Read data from ViewState"
            OnClick="Button2_Click"
            Font-Bold="true"
            ForeColor="HotPink"
            />
    </div>
    </form>
</body>
</html>


More asp.net examples