private void BindCustomSettings() { if (_activeSection.Settings.Count > 0) { foreach (ModuleSetting ms in _activeSection.ModuleType.ModuleSettings) { Control ctrl = TemplateControl.FindControl(ms.Name); if (_activeSection.Settings[ms.Name] != null) { string settingValue = _activeSection.Settings[ms.Name].ToString(); if (ctrl is TextBox) { ((TextBox)ctrl).Text = settingValue; } else if (ctrl is CheckBox) { ((CheckBox)ctrl).Checked = Boolean.Parse(settingValue); } else if (ctrl is DropDownList) { DropDownList ddl = (DropDownList)ctrl; ListItem li = ddl.Items.FindByValue(settingValue); if (li != null) { li.Selected = true; } } } } } }
private void SetCustomSettings() { foreach (ModuleSetting ms in _activeSection.ModuleType.ModuleSettings) { Control ctrl = TemplateControl.FindControl(ms.Name); object val = null; if (ctrl is TextBox) { string text = ((TextBox)ctrl).Text; if (ms.IsRequired && text == String.Empty) { throw new Exception(String.Format("The value for {0} is required.", ms.FriendlyName)); } val = text; } else if (ctrl is CheckBox) { val = ((CheckBox)ctrl).Checked; } else if (ctrl is DropDownList) { val = ((DropDownList)ctrl).SelectedValue; } else if (ctrl is CustomTypeSettingControl) { val = ((CustomTypeSettingControl)ctrl).SelectedValue; } try { // Check if the datatype is correct -> brute force casting :) Type type = ms.GetRealType(); if (type.IsEnum && val is string) { val = Enum.Parse(type, val.ToString()); } else if (type.IsSubclassOf(typeof(EnumrableSetting))) { // There is no need to worry, but if we can check, it's better } else if (type.IsSubclassOf(typeof(TreeViewSetting))) { // There is no need to worry, but if we can check, it's better } else { if (val.ToString().Length > 0) { object testObj = Convert.ChangeType(val, type); } } } catch (InvalidCastException ex) { throw new Exception(String.Format("Giá trị nhập cho {0}: {1} không hợp lệ", ms.FriendlyName, val), ex); } _activeSection.Settings[ms.Name] = val.ToString(); } }