C# 控件和实体相互映射
1 public sealed class WebControlsHelper 2 { 3 #region "控件赋值" 4 /// <summary> 5 /// 设置页面控件的值 6 /// </summary> 7 /// <param name="page"></param> 8 /// <param name="model"></param> 9 public static void SetWebControls<T>(Control page, T model) where T : class, new() 10 { 11 SetWebControls(page, GetModelToHashtable(model)); 12 } 13 /// <summary> 14 /// 实体类Model转Hashtable(反射) 15 /// </summary> 16 public static Hashtable GetModelToHashtable<T>(T model) 17 { 18 Hashtable ht = new Hashtable(); 19 PropertyInfo[] properties = model.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public); 20 foreach (PropertyInfo item in properties) 21 { 22 string key = item.Name; 23 ht[key] = item.GetValue(model, null); 24 } 25 return ht; 26 } 27 /// <summary> 28 /// 设置页面控件的值 29 /// </summary> 30 /// <param name="page"></param> 31 /// <param name="ht"></param> 32 public static void SetWebControls(Control page, Hashtable ht) 33 { 34 if (ht.Count != 0) 35 { 36 int size = ht.Keys.Count; 37 foreach (string key in ht.Keys) 38 { 39 object val = ht[key]; 40 if (val != null) 41 { 42 Control control = page.FindControl(key); 43 Control hiddenCtr = page.FindControl("h" + key); 44 //var s = control.NamingContainer; 45 46 #region hidden input 47 if (hiddenCtr != null) 48 { 49 HtmlInputHidden hInput = (HtmlInputHidden)hiddenCtr; 50 if (hInput != null) 51 { 52 hInput.Value = val.ToString().Trim(); 53 } 54 } 55 #endregion 56 57 #region normal control 58 if (control == null) continue; 59 if (control is HtmlGenericControl) 60 { 61 HtmlGenericControl txt = (HtmlGenericControl)control; 62 txt.InnerHtml = val.ToString().Trim(); 63 } 64 if (control is HtmlInputText) 65 { 66 HtmlInputText txt = (HtmlInputText)control; 67 txt.Value = val.ToString().Trim(); 68 } 69 if (control is TextBox) 70 { 71 TextBox txt = (TextBox)control; 72 txt.Text = val.ToString().Trim(); 73 } 74 if (control is HtmlSelect) 75 { 76 HtmlSelect txt = (HtmlSelect)control; 77 78 if (val.ToString().Trim().ToUpper() == "TRUE" 79 || val.ToString().Trim().ToUpper() == "FALSE") 80 { 81 txt.Value = ((val.ToString().Trim().ToUpper() == "TRUE") ? "1" : "0"); 82 } 83 else 84 { 85 txt.Value = val.ToString().Trim(); 86 } 87 } 88 if (control is HtmlInputHidden) 89 { 90 HtmlInputHidden txt = (HtmlInputHidden)control; 91 txt.Value = val.ToString().Trim(); 92 } 93 if (control is HtmlInputPassword) 94 { 95 HtmlInputPassword txt = (HtmlInputPassword)control; 96 txt.Value = val.ToString().Trim(); 97 } 98 if (control is Label) 99 { 100 Label txt = (Label)control; 101 txt.Text = val.ToString().Trim(); 102 } 103 if (control is HtmlInputCheckBox) 104 { 105 HtmlInputCheckBox chk = (HtmlInputCheckBox)control; 106 chk.Checked = Convert.ToInt32(val) == 1 ? true : false; 107 } 108 if (control is HtmlTextArea) 109 { 110 HtmlTextArea area = (HtmlTextArea)control; 111 area.Value = val.ToString().Trim(); 112 } 113 if (control is DropDownList) 114 { 115 DropDownList drp = (DropDownList)control; 116 drp.SelectedValue = val.ToString().Trim(); 117 } 118 #endregion 119 } 120 } 121 } 122 } 123 #endregion 124 #region "控件获值" 125 /// <summary> 126 /// 获取服务器控件值 127 /// </summary> 128 /// <param name="page"></param> 129 /// <returns></returns> 130 public static Hashtable GetWebControls(Control page) 131 { 132 Hashtable ht = new Hashtable(); 133 int size = HttpContext.Current.Request.Params.Count; 134 for (int i = 0; i < size; i++) 135 { 136 string id = HttpContext.Current.Request.Params.GetKey(i); 137 Control control = page.FindControl(id); 138 if (control == null) continue; 139 control = page.FindControl(id); 140 if (control is HtmlInputText) 141 { 142 HtmlInputText txt = (HtmlInputText)control; 143 ht[txt.ID] = txt.Value.Trim(); 144 } 145 if (control is HtmlSelect) 146 { 147 HtmlSelect txt = (HtmlSelect)control; 148 ht[txt.ID] = txt.Value.Trim(); 149 } 150 if (control is HtmlInputHidden) 151 { 152 HtmlInputHidden txt = (HtmlInputHidden)control; 153 ht[txt.ID] = txt.Value.Trim(); 154 } 155 if (control is HtmlInputPassword) 156 { 157 HtmlInputPassword txt = (HtmlInputPassword)control; 158 ht[txt.ID] = txt.Value.Trim(); 159 } 160 if (control is HtmlInputCheckBox) 161 { 162 HtmlInputCheckBox chk = (HtmlInputCheckBox)control; 163 ht[chk.ID] = chk.Checked ? 1 : 0; 164 } 165 if (control is HtmlTextArea) 166 { 167 HtmlTextArea area = (HtmlTextArea)control; 168 ht[area.ID] = area.Value.Trim(); 169 } 170 } 171 return ht; 172 } 173 #endregion 174 }
人的潜能是能够挖掘的,当你说太晚了的时候,你一定要谨慎,它可能是你退却的借口,没有谁能够阻止你成功,除了你自己,该炫自己的时候,千万别对自己手软!