Skip to content

Instantly share code, notes, and snippets.

@jpoehls
Created October 5, 2009 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpoehls/201768 to your computer and use it in GitHub Desktop.
Save jpoehls/201768 to your computer and use it in GitHub Desktop.
Clears ASP.NET control values
public static void ClearControlValues(
Control parentCtrl, bool recursive)
{
ClearControlValues(parentCtrl, recursive,
new List<Control>(0));
}
public static void ClearControlValues(
Control parentCtrl, bool recursive,
List<Control> ignoreControls)
{
foreach (Control ctrl in parentCtrl.Controls)
{
bool skipMe = false;
foreach (Control ignoreCtrl in ignoreControls)
{
if (ctrl.ID == ignoreCtrl.ID)
{
skipMe = true;
break;
}
}
if (skipMe)
continue;
if (ctrl.GetType() == typeof(TextBox))
((TextBox)ctrl).Text = string.Empty;
if (ctrl.GetType() == typeof(DropDownList))
((DropDownList)ctrl).ClearSelection();
if (ctrl.GetType() == typeof(RadioButton))
((RadioButton)ctrl).Checked = false;
if (ctrl.GetType() == typeof(CheckBox))
((CheckBox)ctrl).Checked = false;
if (ctrl.GetType() == typeof(RadioButtonList))
((RadioButtonList)ctrl).ClearSelection();
if (ctrl.GetType() == typeof(CheckBoxList))
((CheckBoxList)ctrl).ClearSelection();
if (ctrl.GetType() == typeof(ListBox))
((ListBox)ctrl).ClearSelection();
if (recursive == true)
{
ClearControlValues(ctrl, true,
ignoreControls);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment