Search This Blog

Tuesday, July 6, 2010

Clear Controls- ASP.Net

private void ClearControls(Control parent)
{
foreach (Control _ChildControl in parent.Controls)
{
if ((_ChildControl.Controls.Count > 0))
{
ClearControls(_ChildControl);
}
else
{
if (_ChildControl is TextBox)
{
((TextBox)_ChildControl).Text = string.Empty;
}
if (_ChildControl is DropDownList)
{
((DropDownList)_ChildControl).SelectedIndex = 0;
}


}
}
}

----------------------------------------------------------------------------------
private void ResetFormControlValues(Control parent)
{
foreach (Control c in parent.Controls)
{
if (c.Controls.Count > 0)
{
ResetFormControlValues(c);
}
else
{
switch(c.GetType().ToString())
{
case "System.Web.UI.WebControls.TextBox":
((TextBox)c).Text = "";
break;
case "System.Web.UI.WebControls.CheckBox":
((CheckBox)c).Checked = false;
break;
case "System.Web.UI.WebControls.RadioButton":
((RadioButton)c).Checked = false;
break;

}
}
}
}
----------------------------------------------------------------
protected void btnClearASP_Click(object sender, EventArgs e)
{
ResetFormControlValues(this);
}

No comments:

Post a Comment