Neo
Neo

Reputation: 16219

How to I carried out Textbox value from ViewState when I postback?

I want to know that how can I trace out the value of Textbox from ViewState.

As user enters any value into Textbox and click submit button because of postback Textbox value disappears ,

But if I used ViewState in this case , then is there any way to see or display that value from Viewstate?

<html>
<body>
    <form id="form1" runat="server">
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /
    </form>
</body>
</html>

protected void Button1_Click(object sender, EventArgs e)
{
    TextBox1.Text += "X";
}

Upvotes: 0

Views: 2132

Answers (1)

Anıl Canlı
Anıl Canlı

Reputation: 434

In your page load use this.

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (ViewState["Values"] == null)
            {
                ViewState["Values"] = new string();
            }
        }

        TextBox1.Text = ViewState["Values"].ToString();
    }

After that use this.

protected void Button1_Click(object sender, EventArgs e)
    {
        ViewState["Values"] += TextBox1.Text;
    }

In the first Page_Load method, You will create a ViewState if its not a postback and its null. After that write the textbox your viewstate, in Button1_Click you will add your new textbox1 to your viewstate.

Upvotes: 1

Related Questions