Skip to content

Instantly share code, notes, and snippets.

@jaypeeZero
Created December 27, 2012 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jaypeeZero/4392721 to your computer and use it in GitHub Desktop.
Save jaypeeZero/4392721 to your computer and use it in GitHub Desktop.
public partial class MyWebPage {
public void SomeMethod() {
var imaginaryValues = /* Some long string array of values */
Page.GetWebValueControls().ForEach(c => {
c._getOrSetControlValue(true, imaginaryValues[THE_CORRECT_INDEX]);
});
}
}
public static class Extensions {
public static IEnumerable<Control> GetWebValueControls(this Control control) {
return (from n in control.Controls.Cast<Control>().Descendants(c => c.Controls.Cast<Control>())
where (n as TextBox != null
|| n as HtmlInputText != null
|| n as HtmlInputHidden != null
|| n as HtmlInputCheckBox != null
|| n as HtmlInputRadioButton != null
|| n as CheckBox != null
|| n as CheckBoxList != null
|| n as RadioButton != null
|| n as RadioButtonList != null
|| n as DropDownList != null
|| n as HtmlSelect != null
|| n as HtmlTextArea != null
|| n as Calendar != null)
select n);
}
public static IEnumerable<T> Descendants<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> DescendBy) {
foreach (T value in source) {
yield return value;
foreach (T child in DescendBy(value).Descendants<T>(DescendBy)) {
yield return child;
}
}
}
private static string _getOrSetControlValue(this Control control, bool set = false, string val = "") {
var ctrltype = control
.GetType()
.ToString()
.ToLower()
.Replace("system.web.ui.", "")
.Replace("htmlcontrols.", "")
.Replace("webcontrols.", "");
var value = "";
switch (ctrltype) {
case "textbox":
if (set) ((TextBox)control).Text = val;
value = ((TextBox)control).Text;
break;
case "htmlinputtext":
if (set) ((HtmlInputText)control).Value = val;
value = ((HtmlInputText)control).Value;
break;
case "htmlinputhidden":
if (set) ((HtmlInputHidden)control).Value = val;
value = ((HtmlInputHidden)control).Value;
break;
case "htmltextarea":
if (set) ((HtmlTextArea)control).Value = val;
value = ((HtmlTextArea)control).Value;
break;
case "htmlinputcheckbox":
if (set) ((HtmlInputCheckBox)control).Checked = bool.Parse(val);
value = ((HtmlInputCheckBox)control).Checked.ToString();
break;
case "htmlinputradiobutton":
if (set) ((HtmlInputRadioButton)control).Checked = bool.Parse(val);
value = ((HtmlInputRadioButton)control).Checked.ToString();
break;
case "checkbox":
if (set) ((CheckBox)control).Checked = bool.Parse(val);
value = ((CheckBox)control).Checked.ToString();
break;
case "radiobutton":
if (set) ((RadioButton)control).Checked = bool.Parse(val);
value = ((RadioButton)control).Checked.ToString();
break;
case "checkboxlist":
if (set) ((CheckBoxList)control).SelectedValue = val;
value = ((CheckBoxList)control).SelectedValue;
break;
case "radiobuttonlist":
if (set) ((RadioButtonList)control).SelectedValue = val;
value = ((RadioButtonList)control).SelectedValue;
break;
case "dropdownlist":
if (set) ((DropDownList)control).SelectedValue = val;
value = ((DropDownList)control).SelectedValue;
break;
case "htmlselect":
if (set) ((HtmlSelect)control).Value = val;
value = ((HtmlSelect)control).Value;
break;
case "calendar":
if (set && val._decodeString() != "1/1/0001") ((Calendar)control).SelectedDate = DateTime.Parse(val);
value = ((Calendar)control).SelectedDate.ToShortDateString();
break;
default:
value = "";
break;
}
return value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment