惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LINUX DO - 热门话题
Stack Overflow Blog
Stack Overflow Blog
B
Blog
WordPress大学
WordPress大学
Project Zero
Project Zero
P
Palo Alto Networks Blog
阮一峰的网络日志
阮一峰的网络日志
博客园 - 司徒正美
有赞技术团队
有赞技术团队
S
SegmentFault 最新的问题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
小众软件
小众软件
T
Tailwind CSS Blog
Forbes - Security
Forbes - Security
F
Full Disclosure
SecWiki News
SecWiki News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Hacker News: Ask HN
Hacker News: Ask HN
C
Check Point Blog
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
F
Fortinet All Blogs
Cisco Talos Blog
Cisco Talos Blog
G
Google Developers Blog
J
Java Code Geeks
Google DeepMind News
Google DeepMind News
人人都是产品经理
人人都是产品经理
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recorded Future
Recorded Future
O
OpenAI News
Spread Privacy
Spread Privacy
MongoDB | Blog
MongoDB | Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
C
Cybersecurity and Infrastructure Security Agency CISA
S
Securelist
V
Vulnerabilities – Threatpost
Y
Y Combinator Blog
IT之家
IT之家
U
Unit 42
腾讯CDC
S
Security Affairs
C
Cisco Blogs
Schneier on Security
Schneier on Security
The Last Watchdog
The Last Watchdog
B
Blog RSS Feed
宝玉的分享
宝玉的分享
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security @ Cisco Blogs
Cyberwarzone
Cyberwarzone
T
The Blog of Author Tim Ferriss

博客园 - 王德田

lookUpEdit 绑定数据的使用 解除劳动合同是如何赔偿的 水晶报表运行环境包 改iis的asp.net版本命令 NewtonJSONHelper 正则表达式全部符号解释 jquery 获取select 选择的文本 sqlserver 将数据导成insert语句存储过程 整库 linq 反匹配 this.EditingArea' 为空或不是对象 setInterval 例子 C# 字符串 转byte 数组和byte数组转 字符串函数 JSONConverter 查询oracle 用户的存储过程 update 关联表 更新 sql多条件检索 正则表达式介绍 电容的种类与用途,基本知识 电容介绍|电容的种类和作用
将页面的控件值给实体自动赋值
王德田 · 2013-05-24 · via 博客园 - 王德田

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