I posted this on my blog about resetting form fields:
A coworker and I were working on a form and found it to be quite cumbersome to do it field by field. here is some code we found that works to reset all the fields in a form. Add more types in the case statement to reset more items:
void resetField(object myObj)
{
RadioButtonList rl;
string temp = myObj.GetType().ToString();
switch (myObj.GetType().ToString())
{
case "System.Web.UI.WebControls.TextBox":
((TextBox)myObj).Text = "";
break;
case "System.Web.UI.WebControls.CheckBox":
((CheckBox)myObj).Checked = false;
break;
case "System.Web.UI.WebControls.RadioButtonList":
rl = (RadioButtonList)myObj;
if (rl.SelectedItem != null)
rl.SelectedItem.Selected = false;
break;
default:
break;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (Control wc in Page.Form.Controls)
{
resetField(wc);
}
}
Please mark as answered if I helped.
I don't answer personal emails unless I know you or of you. Feel free to post in the forum to get an answer from me.