I tried to write a little function to reset the form to default. Therefore, I want to access the Controls of the page. I'm using a MasterPage. Maybe because of that, I have no access to the ContolsCollection via Page.Controls.
Any solutions for that?
Here the solution:
You have to iterate all controls and check is they have controls themselves. So you do this recursive:
public void ResetForm(ControlCollection objSiteControls)
{
foreach (Control objCurrControl in objSiteControls)
{
string strCurrControlName = objCurrControl.GetType().Name;
if (objCurrControl.HasControls())
{
ResetForm(objCurrControl.Controls);
}
switch (strCurrControlName)
{
case "TextBox":
TextBox objTextBoxControl = (TextBox)objCurrControl;
objTextBoxControl.Text = string.Empty;
break;
case "DropDownList":
DropDownList objDropDownControl = (DropDownList)objCurrControl;
objDropDownControl.SelectedIndex = -1;
break;
case "GridView":
GridView objGridViewControl = (GridView)objCurrControl;
objGridViewControl.SelectedIndex = -1;
break;
case "CheckBox":
CheckBox objCheckBoxControl = (CheckBox)objCurrControl;
objCheckBoxControl.Checked = false;
break;
case "CheckBoxList":
CheckBoxList objCheckBoxListControl = (CheckBoxList)objCurrControl;
objCheckBoxListControl.ClearSelection();
break;
case "RadioButtonList":
RadioButtonList objRadioButtonList = (RadioButtonList)objCurrControl;
objRadioButtonList.ClearSelection();
break;
}
}
}