Created
October 5, 2009 02:17
-
-
Save jpoehls/201768 to your computer and use it in GitHub Desktop.
Clears ASP.NET control values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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