將頁面的控制項值給實體自動賦值

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.