Thursday, December 2, 2010

Clear all the TextBoxes in a form

What are we cooking today?
How to clear TextBoxes in a form that uses masterpages. (C#)


Recipe

This procedure clears all the textboxes in a form. Please note this works when using a masterpage:
  
    public void ClearTextboxesInForm(string strContentPlaceHolder)
    {
        foreach (Control control in Form.Controls) 
        {
            if (control.ID == strContentPlaceHolder) 
            {
                foreach (Control ctrl in control.Controls)
                {
                    if (ctrl is TextBox)  //---Find the textboxes 
                    {
                        ((TextBox)ctrl).Text = "";
                    }
                }
            }
        }
    }