Friday, January 4, 2008

How To Clear All Textboxes in ASP.NET

private void Button1_Click(object sender, System.EventArgs e)
    {
        Control myForm = Page.FindControl("Form1");

        foreach (Control ctl in myForm.Controls)
        {
            if (ctl.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox"))
            {
                ((TextBox)ctl).Text = "";
            }
        }
    }
This will clear EVERYTHING from the textboxes - even if you had them pre-populated with data. A simple way to just reset it to the condition at Page_Load time, just do this in the Reset SubRoutine: Server.Transfer("YourPageName.aspx")

No comments:

Post a Comment