Code to clear all Textboxes with single button click

In Our applications in most of the cases we have to clear all text boxes (when Cancel or Reset button is clicked)
This code is to clear all text boxes in the form .

Use This code in Button click event


public void imgcancel_Click(object sender, ImageClickEventArgs e)
{
FindControl(Form.Controls);
}





Find Control Method Definition

public static void FindControl(ControlCollection ChildCtrls)
{
foreach (Control Ctrl in ChildCtrls)
{
if (Ctrl is TextBox)
((TextBox)Ctrl).Text = "";
else
FindControl(Ctrl.Controls);
}
}




Regards
Lalitha


Comments

Author: Komathi Sasikumar20 Nov 2008 Member Level: Silver   Points : 2

VB code to clear the textboxes in a groupbox in a single button click:

1.Place few textboxes inside a groupbox.
2.Place a button inside/outside the groupbox.(your wish)

Use the following code in button click:


Dim Form_control As New Control
For Each Form_control In GroupBox1.Controls
If TypeOf (Form_control) Is TextBox Then
Form_control.Text = ""
End If
Next

Author: madhuri22 Nov 2008 Member Level: Bronze   Points : 2

hiii


public void ClearControls(Control Parent)
{
if (Parent is TextBox)
{
((TextBox)Parent).Text = "";
}
if (Parent is DropDownList)
{
((DropDownList)Parent).SelectedIndex = 0;
}
foreach (Control ctrl in Parent.Controls)
{
ClearControls(ctrl);
}
}



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: