<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Reflection;
    using System.IO;
    using System.Text;

    namespace ProjectDemo.Common
    {
        public static class EntityCopy
        {
            
             #region
             /// <summary>

            /// 通過搜索頁面對應控件來構造Model對象(要求控件必須以“_Model的屬性名”來命名(如:_Name),并大小寫一致)

            /// </summary>

            /// <param name="model">要構造的Model對象()</param>

            /// <param name="parentControl">控件的容器(比如Page或者Master的站位控件)</param>

            /// <returns>返回參數里model</returns>

            public static T GetModel<T>(T model, Control parentControl)

            {

                Type t = model.GetType();

                PropertyInfo[] pi = t.GetProperties();

                foreach (PropertyInfo p in pi)

                {

                    SetControlValueToModel(model, p, parentControl);

                }

                return model;

            }

     

            /// <summary>

            /// 把頁面控件上的值賦值給Model對象(要求控件必須以“_Model的屬性名”來命名(如:_Name),并大小寫一致)

            /// </summary>

            /// <param name="model">要賦值的Model對象</param>

            /// <param name="p">某個屬性</param>

            /// <param name="parentControl">控件的容器(比如Page或者Master的站位控件)</param>

            private static void SetControlValueToModel(object model, PropertyInfo p, Control parentControl)

            {

                Control control = parentControl.FindControl("_" + p.Name);

                if (control != null)

                {

                    Type t_c = control.GetType();

                    switch (t_c.FullName)

                    {

                        case "System.Web.UI.WebControls.TextBox": SetValue(model, p, ((TextBox)control).Text); break;

                        case "System.Web.UI.WebControls.CheckBox": SetValue(model, p, ((CheckBox)control).Checked); break;

                        case "System.Web.UI.WebControls.CheckBoxList":

                            CheckBoxList cbl = ((CheckBoxList)control);

                            StringBuilder sb = new StringBuilder();

                            for (int i = 0; i < cbl.Items.Count; i++)

                            {

                                if (cbl.Items[i].Selected)

                                {

                                    sb.Append(i);

                                    sb.Append(",");

                                }

                            }

                            SetValue(model, p, sb.ToString().TrimEnd(',')); break;

                        case "System.Web.UI.WebControls.Image": SetValue(model, p, ((Image)control).ImageUrl); break;

                        case "System.Web.UI.WebControls.DropDownList": SetValue(model, p, ((DropDownList)control).SelectedValue); break;

                        case "System.Web.UI.WebControls.RadioButtonList": SetValue(model, p, ((RadioButtonList)control).SelectedValue); break;

                        case "System.Web.UI.WebControls.HiddenField": SetValue(model, p, ((HiddenField)control).Value); break;

                        default: break;

                    }

                }

            }

     

            /// <summary>

            /// 把值賦給指定Model對象指定屬性上

            /// </summary>

            /// <param name="model">需要賦值的Model對象</param>

            /// <param name="p">某個屬性</param>

            /// <param name="value">要賦給Model的屬性的值</param>

            private static void SetValue(object model, PropertyInfo p, object value)

            {

                if (p.PropertyType.FullName == "System.Guid")

                {

                    p.SetValue(model, new Guid(value.ToString()), null);

                }

                else

                {

                    p.SetValue(model, Convert.ChangeType(value, p.PropertyType), null);

                }

            }

     

            #endregion

     

            #region 反射Model綁定頁面控件

     

            /// <summary>

            /// 綁定Model的值到頁面上對應控件(要求控件必須以“_Model的屬性名”來命名(如:_Name),并大小寫一致)

            /// </summary>

            /// <param name="model">賦好值的Model</param>

            /// <param name="parentControl">控件的容器(比如Page或者Master的站位控件)</param>

            public static void BindControls(object model, Control parentControl)

            {

                Type t = model.GetType();

                PropertyInfo[] pi = t.GetProperties();

                foreach (PropertyInfo p in pi)

                {

                    SetModelValueToControl(model, p, parentControl);

                }

            }

     

            /// <summary>

            /// 把Model的值賦給頁面上的控件(目前只針對Web)

            /// </summary>

            /// <param name="model">賦好值的Model</param>

            /// <param name="p">Model的某個屬性</param>

            /// <param name="parentControl">控件的容器(比如Page或者Master的站位控件)</param>

            private static void SetModelValueToControl(object model, PropertyInfo p, Control parentControl)

            {

                Control control = parentControl.FindControl("_" + p.Name);

                if (control != null)

                {

                    Type t_c = control.GetType();

                    switch (t_c.FullName)

                    {
                        case "System.Web.UI.WebControls.TextBox": ((TextBox)control).Text = p.GetValue(model, null).ToString(); break;
                        case "System.Web.UI.WebControls.Label": ((Label)control).Text = p.GetValue(model, null).ToString(); break;

                        case "System.Web.UI.WebControls.Literal": ((Literal)control).Text = p.GetValue(model, null).ToString(); break;

                      

                        case "System.Web.UI.WebControls.Image": ((Image)control).ImageUrl = p.GetValue(model, null).ToString(); break;

                        case "System.Web.UI.WebControls.DropDownList": ((DropDownList)control).SelectedValue = p.GetValue(model, null).ToString(); break;

                        case "System.Web.UI.WebControls.RadioButtonList": ((RadioButtonList)control).SelectedValue = p.GetValue(model, null).ToString(); break;

                        case "System.Web.UI.WebControls.CheckBox": ((CheckBox)control).Checked = (bool)p.GetValue(model, null); break;

                        case "System.Web.UI.WebControls.CheckBoxList":

                            string[] arr = ((string)p.GetValue(model, null)).Split(',');

                            CheckBoxList cbl = ((CheckBoxList)control);

                            for (int i = 0; i < arr.Length; i++)

                            {

                                cbl.Items[int.Parse(arr[i])].Selected = true;

                            }

                            break;

                        case "System.Web.UI.WebControls.HiddenField": ((HiddenField)control).Value = p.GetValue(model, null).ToString(); break;

                        default: break;

                    }

                }

            }

               #endregion

     


        }
        public class Person
        {
            public string Name { set; get; }
            public string Sex { set; get; }
        }
    }

    posted on 2013-06-09 17:13 sanmao 閱讀(679) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     

    常用鏈接

    留言簿(5)

    隨筆分類

    隨筆檔案

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲日韩在线视频| 亚洲男人都懂得羞羞网站| 国产成人精品亚洲日本在线| 亚洲高清资源在线观看| 免费一区二区无码东京热| 国产亚洲精品岁国产微拍精品| 亚洲熟妇色自偷自拍另类| 看亚洲a级一级毛片| 无码国产精品一区二区免费式芒果| 免费网站看v片在线香蕉| 亚洲1区2区3区精华液| 日本久久久免费高清| 久久久久亚洲精品影视| 国产精品亚洲一区二区三区| 日韩精品无码专区免费播放| 亚洲第一页综合图片自拍| 春意影院午夜爽爽爽免费| 中文字幕亚洲图片| 免费在线中文日本| 亚洲成人网在线观看| 免费毛片在线播放| 产传媒61国产免费| 亚洲福利视频导航| 啦啦啦高清视频在线观看免费| 久久精品国产亚洲AV香蕉| 无码人妻一区二区三区免费手机| 久久精品国产亚洲AV果冻传媒| 一级毛片免费播放视频| 亚洲精品乱码久久久久久| 成年网站免费视频A在线双飞| 亚洲精品国产手机| 久草福利资源网站免费| 国内精品久久久久影院亚洲| 亚洲国产成人爱av在线播放| 日本免费一区二区三区| 亚洲乱妇老熟女爽到高潮的片| 四虎在线免费视频| 立即播放免费毛片一级| 久久久无码精品亚洲日韩蜜臀浪潮| 免费人成在线观看视频高潮| 亚洲免费福利在线视频|