0
votes

I have a textbox and a button on the ascx page.

After entering text i click on the button where it will post to the database and clear the textbox.

After positing, if i refresh the web page it is going to the button click method and posting the old text to the database. how can i clear the textbox value from the view state.

i have set the enableviewstate of the textbox to false. But still it is not working. Please let me know. Thanks

5
EnableViewState is ignored for child controls. It won't work until .NET 4.0. - Cᴏʀʏ
Did you try to put your textbox in updatepanel ? - Cédric Boivin
When you say refresh, I assume that you are talking about a browser refresh, in which case, I'm guessing the browser says that to reload the page some data must be posted back? That is the browser repeating the previous post that got you to the page you are viewing, and that doesn't have anything to do with the ViewState. If you are clicking the button again, that's a different problem. Which is it? - NerdFury
Its the first one its a browser refresh. - vamsivanka

5 Answers

1
votes
protected void Button_Click(object sender, EventArgs e)
{
   var txt=textBox1.Text;
   textBox1.Text="";//Set it to empty

   // do other stuff
   ............ 
   ............
}
1
votes
protected void btnClear_Click(object sender, EventArgs e)
    {
        ClearControls();

    }
    private void ClearControls()
    {
        foreach (Control c in Page.Controls)
        {
            foreach (Control ctrl in c.Controls)
            {
                if (ctrl is TextBox)
                {
                    ((TextBox)ctrl).Text = string.Empty;
                }
            }
        }
    }
0
votes

After you save your data with the click button you can set the textbox.text = ""

EDIT: After your comment that textbox.text = "" is not working....

When you hit the refresh it sounds like it is resubmitting your work again. Try just reloading the page by browsing to your page again.

Also be sure to check that you aren't saving your data on every postback but just on the button click event.

Do you have any code in your page load event?

0
votes

Long shot here, but this worked in my case:

        TextBox.Attributes.Remove("value");

This was on a textbox with the text mode sent to 'Password'.

0
votes

just add this at the end of the event after page i going o be refresh

Response.Redirect("Registration.aspx");

here my WebForm name is "Registration.aspx" instead of this put your WebForm name.