Wednesday, May 14, 2008

How to Clear all Text in a TextBox in C#

Im using C sharp 3.0

Try This Codes:


private void button1_Click(object sender, EventArgs e)
{
foreach(Control sayre in this.Controls)
{
if (sayre is TextBox)
{
(sayre as TextBox).Clear();
}
}
}

3 comments:

elizabeth said...

worked correctly...great work

Chandan Pasunoori said...

private void ClearTextBoxes(Control control)
{
foreach (Control c in control.Controls)
{
if (c is TextBox)
{
((TextBox)c).Clear();
}

if (c.HasChildren)
{
ClearTextBoxes(c);
}


if (c is CheckBox)
{

((CheckBox)c).Checked = false;
}

if (c is RadioButton)
{
((RadioButton)c).Checked = false;
}
}
}

zocerus said...

can u pls ,mention the reference
used,lik 'using System.Web.UI.WebControls' .which s the reference used in the code u specified...