using System;<br />using System.Web;<br />using System.Web.UI;<br />using System.Web.UI.HtmlControls;<br />using System.Web.UI.WebControls;<br />using System.Reflection;</p><p>//==========================<br />//author:Dy<br />//date:20080329<br />//==========================<br />namespace BLL<br />{<br /> /// <summary><br /> /// 說明: 利用反射將業務對象和控制項綁定<br /> /// 本類有兩個靜態方法 <see cref="BindObjectToControls"/> 和 <see cref="BindControlsToObject"/><br /> /// 用來繫結控制項值或對象屬性 <see cref="Control"/><br /> /// 控制項ID 要和 業務對象屬性名稱 相同<br /> ///<br /> /// </summary><br /> public class FormBinding<br /> {<br /> /// <summary><br /> /// 綁定對象屬性 的值到同名控制項 <see cref="Control"/><br /> /// </summary><br /> /// <param name="obj">屬性要綁定表單控制項的對象</param><br /> /// <param name="container">控制項容器 通常是Page 頁面 </param><br /> public static void BindObjectToControls(object obj, Control container)<br /> {<br /> if (obj == null) return;</p><p> // 得到業務對象屬性<br /> //<br /> Type objType = obj.GetType();<br /> PropertyInfo[] objPropertiesArray = objType.GetProperties();</p><p> foreach (PropertyInfo objProperty in objPropertiesArray)<br /> {</p><p> Control control = container.FindControl(objProperty.Name);</p><p> // 清單控制項 (DropDownList, CheckBoxList, RadioButtonList)<br /> //<br /> if (control is ListControl)<br /> {<br /> ListControl listControl = (ListControl)control;<br /> foreach (ListItem lItem in listControl.Items)<br /> {<br /> lItem.Selected = false;<br /> }<br /> string propertyValue = objProperty.GetValue(obj, null).ToString();<br /> string[] s = propertyValue.Split(',');<br /> foreach (string i in s)<br /> {<br /> ListItem listItem = listControl.Items.FindByText(i);<br /> if (listItem != null) listItem.Selected = true;<br /> }</p><p> }<br /> else if (control != null)<br /> {<br /> // 得到控制項屬性<br /> //<br /> Type controlType = control.GetType();<br /> PropertyInfo[] controlPropertiesArray = controlType.GetProperties();</p><p> //<br /> // 測試通用屬性<br /> bool success = false;<br /> success = FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "Checked", typeof(bool));</p><p> if (!success)<br /> success = FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "SelectedDate", typeof(DateTime));</p><p> if (!success)<br /> success = FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "Value", typeof(String));</p><p> if (!success)<br /> success = FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "Text", typeof(String));</p><p> if (!success)<br /> success = FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "InnerHtml", typeof(String));</p><p> }<br /> }<br /> }</p><p> /// <summary><br /> /// 尋找控制項屬性和類型並嘗試給業務對象同名屬性賦值<br /> /// </summary><br /> /// <param name="obj">已找到屬性的對象</param><br /> /// <param name="objProperty">已找到對象 的 屬性</param><br /> /// <param name="control">控制項ID和業務對象屬性名稱匹配的控制項</param><br /> /// <param name="controlPropertiesArray">控制項屬性集合</param><br /> /// <param name="propertyName">要設定的控制項屬性名稱</param><br /> /// <param name="type">控制項屬性類型校正</param><br /> /// <returns>屬性是否被找到並設定</returns><br /> private static bool FindAndSetControlProperty(object obj, PropertyInfo objProperty, Control control, PropertyInfo[] controlPropertiesArray, string propertyName, Type type)<br /> {<br /> // 遍曆控制項屬性<br /> //<br /> foreach (PropertyInfo controlProperty in controlPropertiesArray)<br /> {<br /> // 檢查屬性名稱和類型<br /> //<br /> if (controlProperty.Name == propertyName && controlProperty.PropertyType == type)<br /> {<br /> // 設定控制項屬性的值到業務對象<br /> //<br /> controlProperty.SetValue(control, Convert.ChangeType(objProperty.GetValue(obj, null), type), null);<br /> return true;<br /> }<br /> }<br /> return false;<br /> }</p><p> /// <summary><br /> /// 設定控制項 <see cref="Control"/>s值到業務對象<br /> /// </summary><br /> /// <param name="obj">要綁定的業務對象</param><br /> /// <param name="container">控制項容器</param><br /> public static void BindControlsToObject(object obj, Control container)<br /> {<br /> if (obj == null) return;</p><p> // 擷取業務對象屬性<br /> //<br /> Type objType = obj.GetType();<br /> PropertyInfo[] objPropertiesArray = objType.GetProperties();</p><p> foreach (PropertyInfo objProperty in objPropertiesArray)<br /> {</p><p> Control control = container.FindControl(objProperty.Name);<br /> if (control is ListControl)<br /> {<br /> ListControl listControl = (ListControl)control;<br /> string str="";<br /> if (listControl.SelectedItem != null)</p><p> for (int i = 0; i < listControl.Items.Count; i++)<br /> {<br /> if (listControl.Items[i].Selected)<br /> {<br /> str += listControl.Items[i].Text+",";<br /> }<br /> }<br /> objProperty.SetValue(obj, Convert.ChangeType(str, objProperty.PropertyType), null);</p><p> }<br /> else if (control is HtmlInputHidden)<br /> {</p><p> try<br /> {<br /> HtmlInputHidden hidControl = (HtmlInputHidden)control;<br /> //hidControl.Value = hidControl.Value==""?"0":hidControl.Value;<br /> objProperty.SetValue(obj, Convert.ChangeType(hidControl.Value, objProperty.PropertyType), null);<br /> }<br /> catch<br /> {</p><p> }<br /> }<br /> else if (control is HtmlInputText)<br /> {</p><p> try<br /> {<br /> HtmlInputText txtControl = (HtmlInputText)control;<br /> //txtControl.Value = txtControl.Value==""?"0":txtControl.Value;<br /> objProperty.SetValue(obj, Convert.ChangeType(txtControl.Value, objProperty.PropertyType), null);<br /> }<br /> catch<br /> {</p><p> }<br /> }</p><p> else if (control != null)<br /> {<br /> // 擷取控制項屬性<br /> //<br /> Type controlType = control.GetType();<br /> PropertyInfo[] controlPropertiesArray = controlType.GetProperties();</p><p> // 嘗試通用屬性<br /> //<br /> bool success = false;<br /> success = FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray, "Checked", typeof(bool));</p><p> if (!success)<br /> success = FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray, "SelectedDate", typeof(DateTime));</p><p> if (!success)<br /> success = FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray, "Value", typeof(String));</p><p> if (!success)<br /> success = FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray, "Text", typeof(String));<br /> }<br /> }<br /> }</p><p> /// <summary><br /> /// 尋找控制項值並嘗試賦值給同名業務對象屬性<br /> ///<br /> /// </summary><br /> /// <param name="obj">待賦值的業務對象</param><br /> /// <param name="objProperty">業務對象屬性</param><br /> /// <param name="control">同名控制項</param><br /> /// <param name="controlPropertiesArray">控制項數組</param><br /> /// <param name="propertyName">得到的控制項屬性名稱</param><br /> /// <param name="type">控制項屬性類型</param><br /> /// <returns>屬性是否找到</returns><br /> private static bool FindAndGetControlProperty(object obj, PropertyInfo objProperty, Control control, PropertyInfo[] controlPropertiesArray, string propertyName, Type type)<br /> {<br /> // 遍曆控制項屬性<br /> //<br /> foreach (PropertyInfo controlProperty in controlPropertiesArray)<br /> {<br /> // 檢查名稱和類型<br /> //<br /> if (controlProperty.Name == propertyName && controlProperty.PropertyType == typeof(String))<br /> {<br /> // 控制項賦值給業務對象<br /> //<br /> try<br /> {<br /> Object ctrValue = Convert.ChangeType(controlProperty.GetValue(control, null), objProperty.PropertyType);<br /> objProperty.SetValue(obj, ctrValue, null);</p><p> return true;<br /> }<br /> catch<br /> {<br /> //<br /> // 沒有找到控制項對應的業務對象屬性<br /> return false;<br /> }<br /> }<br /> }<br /> return false;<br /> }<br /> }<br />}<br />
實體類的屬性和控制項的id命名一致既可以了
調用時
賦值,實體類給控制項賦值:BLL.FormBinding.BindObjectToControls(model, this);//model為實體類。
取值,控制項的值賦給實體類:BLL.FormBinding.BindControlsToObject(model, this);
一句話完成取值和賦值。