vamsivanka
vamsivanka

Reputation: 792

clear textbox after postback

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

Upvotes: 0

Views: 13247

Answers (5)

Android villa
Android villa

Reputation: 11

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.

Upvotes: 0

Volearix
Volearix

Reputation: 1593

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'.

Upvotes: 0

Shaik Raffi
Shaik Raffi

Reputation: 174

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;
                }
            }
        }
    }

Upvotes: 1

Kevin LaBranche
Kevin LaBranche

Reputation: 21078

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?

Upvotes: 0

TheVillageIdiot
TheVillageIdiot

Reputation: 40497

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

   // do other stuff
   ............ 
   ............
}

Upvotes: 1

Related Questions