<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>

            /// 通過(guò)搜索頁(yè)面對(duì)應(yīng)控件來(lái)構(gòu)造Model對(duì)象(要求控件必須以“_Model的屬性名”來(lái)命名(如:_Name),并大小寫(xiě)一致)

            /// </summary>

            /// <param name="model">要構(gòu)造的Model對(duì)象()</param>

            /// <param name="parentControl">控件的容器(比如Page或者M(jìn)aster的站位控件)</param>

            /// <returns>返回參數(shù)里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>

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

            /// </summary>

            /// <param name="model">要賦值的Model對(duì)象</param>

            /// <param name="p">某個(gè)屬性</param>

            /// <param name="parentControl">控件的容器(比如Page或者M(jìn)aster的站位控件)</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對(duì)象指定屬性上

            /// </summary>

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

            /// <param name="p">某個(gè)屬性</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綁定頁(yè)面控件

     

            /// <summary>

            /// 綁定Model的值到頁(yè)面上對(duì)應(yīng)控件(要求控件必須以“_Model的屬性名”來(lái)命名(如:_Name),并大小寫(xiě)一致)

            /// </summary>

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

            /// <param name="parentControl">控件的容器(比如Page或者M(jìn)aster的站位控件)</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的值賦給頁(yè)面上的控件(目前只針對(duì)Web)

            /// </summary>

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

            /// <param name="p">Model的某個(gè)屬性</param>

            /// <param name="parentControl">控件的容器(比如Page或者M(jìn)aster的站位控件)</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 閱讀(689) 評(píng)論(0)  編輯  收藏

    只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     

    常用鏈接

    留言簿(5)

    隨筆分類(lèi)

    隨筆檔案

    搜索

    •  

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 亚洲精品无码永久中文字幕| 国产成人啪精品视频免费网| 亚洲午夜日韩高清一区| 亚洲人午夜射精精品日韩| 国产AV无码专区亚洲AV蜜芽 | 鲁丝片一区二区三区免费 | 久久永久免费人妻精品| 亚洲国产精品成人精品无码区| 精品国产麻豆免费人成网站| 久久亚洲国产成人亚| 日本免费人成在线网站| 国产精品国产免费无码专区不卡| 亚洲色偷偷色噜噜狠狠99| 久久成人18免费网站 | 国产禁女女网站免费看| 免费无码婬片aaa直播表情| 国产成人毛片亚洲精品| 大地影院MV在线观看视频免费| 夜夜嘿视频免费看| 国产亚洲视频在线| 女人18毛片特级一级免费视频| 亚洲日韩精品国产一区二区三区| 久爱免费观看在线网站| 亚洲人成电影青青在线播放| 国产禁女女网站免费看| 成人片黄网站色大片免费观看cn| 免费看的黄色大片| 7777久久亚洲中文字幕蜜桃| 一级做a爰性色毛片免费| 四虎在线免费播放| 四虎影视在线看免费观看| 国产亚洲成av人片在线观看| 国产一卡二卡四卡免费| 亚洲AV无码精品无码麻豆| 99久久精品日本一区二区免费| 亚洲av无码av在线播放| 久久精品国产亚洲麻豆| 免费精品一区二区三区在线观看| a毛片久久免费观看| 亚洲精品97久久中文字幕无码| 亚洲视频在线免费观看|