How to clear all server control values using ASP.NET
This method is used for clearing all the .net controls after you click the submit button.
you will not need to write the id and manually assign null or "" to controls value.this will iterate to all the controls of any type like drop down or text box any.
you will not need to write the id and manually assign null or "" to controls value.this will iterate to all the controls of any type weather is contains single value or multiple value, like drop down or text box any.
what you need to do for using this function.
>create a class.
>Mane it mainDatacode
>copy this method to class
>when you want to use this method
create the instance of class like
mainDatacode mdc=new mainDatacode();
and write following in code behind where you want to clear all the controls.
mdc.ClearControls(this);
will clears all the controls value in current page.
Hope this article would be help full to you all.
public 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;
}
else if (_ChildControl is CheckBox)
{
((CheckBox)_ChildControl).Checked = false;
}
else if (_ChildControl.GetType().ToString() == "System.Web.UI.WebControls.DropDownList")
{
DropDownList ddl = (DropDownList)_ChildControl;
if (ddl != null)
{
ddl.ClearSelection();
}
// if //(_ChildControl is DropDownList)
// {
//((DropDownList)_ChildControl).Items.Clear();
//}
}
else if (_ChildControl.GetType().ToString() == "System.Web.UI.WebControls.RadioButton")
{
((RadioButton)_ChildControl).Checked = false;
}
}
}
}