使用反射将业务对象绑定到 ASP.NET 窗体控件(修改篇)

手把手教你双目测距理论及生成视差图,深度图,点云图像 Stereo Vision, 也叫双目立体视觉,它的研究可以帮助我们更好的理解人类的双眼是如何进行深度感知的。双目视觉在许多领域得到了应用,例如城市三维重建、3D模型构建(如kinect fusion)、视角合成、3D跟踪、机器人导航(自动驾驶)、人类运动捕捉(Microsoft Kinect)等等。双目测距也属于双目立体视觉的一个应用领域,双目测距的基本原理主要是三角测量原理,即通过视差来判定物体的远近。 阅读详情

MSDN上面的代码和下载的代码包也有那么多问题,晕死。
综合下载的代码和MSDN上的说明,修改了这段代码,现在能正常使用,特别是很多字段放在窗体上的时候,使用起来非常方便。

具体用法:把窗体上的控件ID属性与实体层的属性设置成一致。
绑定实体层到窗体的时候用:
web.model .test  Test = new web.model.test();
Test = bll.GetModel(id);
BindObjectToControls(model,this);

要把窗体数据绑定到实体层的时候用:
BindControlsToObject(model,this);

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Reflection;

namespace WEB
{
    /// <summary>
    /// A class with two static methods <see cref="BindObjectToControls"/> and <see cref="BindControlsToObject"/>
    /// used to bind the values of and object's properties with web <see cref="Control"/>s that have their
    /// ID property set to the name of the object property.
    /// </summary>
    public class FormBinding
    {
        /// <summary>
        /// Binds an object's properties to <see cref="Control"/>s with the same ID as the propery name.
        /// </summary>
        /// <param name="obj">The object whose properties are being bound to forms Controls</param>
        /// <param name="container">The control in which the form Controls reside (usually a Page or ContainerControl)</param>
        public static void BindObjectToControls(object obj, Control container)
        {
            if (obj == null) return;

            // Get the properties of the business object
            //
            Type objType = obj.GetType();
            PropertyInfo[] objPropertiesArray = objType.GetProperties();

            foreach (PropertyInfo objProperty in objPropertiesArray)
            {

                Control control = container.FindControl(objProperty.Name);
                if (control == null) continue;

                // handle ListControls (DropDownList, CheckBoxList, RadioButtonList)
                //
                if (control is ListControl)
                {
                    ListControl listControl = (ListControl)control;
                    string propertyValue = objProperty.GetValue(obj, null).ToString();
                    ListItem listItem = listControl.Items.FindByValue(propertyValue);
                    if (listItem != null) listItem.Selected = true;

                }
                else
                {
                    // get the properties of the control
                    //
                    Type controlType = control.GetType();
                    PropertyInfo[] controlPropertiesArray = controlType.GetProperties();

                    // test for common properties
                    //
                    bool success = false;
                    success = FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "Checked", typeof(bool));

                    if (!success)
                        success = FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "SelectedDate", typeof(DateTime));

                    if (!success)
                        success = FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "Value", typeof(String));

                    if (!success)
                        success = FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "Text", typeof(String));

                }
            }
        }

        /// <summary>
        /// Looks for a property name and type on a control and attempts to set it to the value in an object's property
        /// of the same name.
        /// </summary>
        /// <param name="obj">The object whose properties are being retrieved</param>
        /// <param name="objProperty">The property of the object being retrieved</param>
        /// <param name="control">The control whose ID matches the object's property name.</param>
        /// <param name="controlPropertiesArray">An array of the control's properties</param>
        /// <param name="propertyName">The name of the Control property being set</param>
        /// <param name="type">The correct type for the Control property</param>
        /// <returns>Boolean for whether the property was found and set</returns>
        private static bool FindAndSetControlProperty(object obj, PropertyInfo objProperty, Control control, PropertyInfo[] controlPropertiesArray, string propertyName, Type type)
        {
            // iterate through control properties
            //
            foreach (PropertyInfo controlProperty in controlPropertiesArray)
            {
                // check for matching name and type
                //
                if (controlProperty.Name == propertyName && controlProperty.PropertyType == type)
                {
                    // set the control's property to the business object property value
                    //
                    controlProperty.SetValue(control, Convert.ChangeType(objProperty.GetValue(obj, null), type), null);
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// Binds your the values in <see cref="Control"/>s to a business object.
        /// </summary>
        /// <param name="obj">The object whose properties are being bound to Control values</param>
        /// <param name="container">The control in which the form Controls reside (usually a Page or ContainerControl)</param>
        public static void BindControlsToObject(object obj, Control container)
        {
            if (obj == null) return;

            // Get the properties of the business object
            //   
            Type objType = obj.GetType();
            PropertyInfo[] objPropertiesArray = objType.GetProperties();

            foreach (PropertyInfo objProperty in objPropertiesArray)
            {

                Control control = container.FindControl(objProperty.Name);
                if (control == null) continue;
                if (control is ListControl)
                {
                    ListControl listControl = (ListControl)control;
                    if (listControl.SelectedItem != null)
                        objProperty.SetValue(obj, Convert.ChangeType(listControl.SelectedItem.Value, objProperty.PropertyType), null);

                }
                else
                {
                    // get the properties of the control
                    //
                    Type controlType = control.GetType();
                    PropertyInfo[] controlPropertiesArray = controlType.GetProperties();

                    // test for common properties
                    //
                    bool success = false;
                    success = FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray, "Checked", typeof(bool));

                    if (!success)
                        success = FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray, "SelectedDate", typeof(DateTime));

                    if (!success)
                        success = FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray, "Value", typeof(String));

                    if (!success)
                        success = FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray, "Text", typeof(String));
                }
            }
        }

        private static bool FindAndGetControlProperty(object obj,PropertyInfo objProperty, Control control, PropertyInfo[] controlPropertiesArray, string propertyName, Type type)
        {
            // 在整个控件属性中进行迭代
            foreach (PropertyInfo controlProperty in controlPropertiesArray)
            {
                // 检查匹配的名称和类型
                if (controlProperty.Name == "Text" && controlProperty.PropertyType == typeof(String))
                {
                    // 将控件的属性设置为
                    // 业务对象属性值
                    try
                    {
                        objProperty.SetValue(obj, Convert.ChangeType(controlProperty.GetValue(control, null), objProperty.PropertyType), null);
                        return true;
                    }
                    catch
                    {
                        // 无法将来自窗体控件
                        // 的数据转换为
                        // objProperty.PropertyType
                        return false;
                    }
                }
            }
            return true;
        }

    }
}
 

SAP Build Work Zone standard edition 订阅报错 OIDC trust missing 的成因与完整解决路径 摘要:用户订阅SAP Build Work Zone时遇到422错误,提示未配置OpenID Connect信任。该错误表明业务用户需通过SAP Cloud Identity Services的OIDC协议认证,而当前子账户仅配置了SAML信任或未配置信任。解决方法是进入子账户的Trust Configuration页面,使用Establish Trust功能建立与SAP Cloud Identity Services的OIDC信任关系。这是SAP新版服务的强制要求,SAML协议已不被支持。 阅读详情

相关推荐

FSL软件安装

FSL软件安装

huang_tianyuan的博客 2630

WinForm开发-界面控件到实体,实体到界面控件自动绑定

  在WinForm开发中,我们是不是为绑定界面控件的数据而每个控件每个控件的赋值?在保存修改时是不是也是每个控件每个控件的赋值到实体中?字段一多,那简直就是噩梦。有没有像Web中那样方便的方法直接就自动映射了呢?现在不用如此繁琐,在RDIFramework.NET开发框架的WinForm部分新增了界面控件到实体,实体到界面控件自动绑定,一句话就搞定了,非常的方便。主要是引用“RDIFramewo...

weixin_33720452的博客 508

LAMMPS实操系列(四): 高熵合金FCC-CoCrCuFeNi的退火模拟

我们对FCC-CoCrCuFeNi的退火模拟思路主要是,首先将材料以均匀的速率用100 ps从300 K升温至1000 K,然后在1000 K的温度下平衡100 ps,随后以均匀的速率用100 ps从1000 K降温至300 K,随后通过共轭梯度下降法做静力学结构优化得到退火后的结构。由于FCC-CoCrCuFeNi高熵合金被认为是潜在的工程结构材料[1],接下来我们将对退火前后的结构进行压缩模拟,对比退火前后FCC-CoCrCuFeNi高熵合金的力学性能变化,敬请各位读者关注我们的更新。

Ceshigo_CD的博客 4837

绑定对象至页面控件(支持可空类型)

  前提:控件名称和对象实体属性名称必须一致(可以带前缀)。   另外支持对象实体属性为可空类型。 public static class Types { public static bool IsNullableType(this Type type) { return (((type != null) && t...

weixin_30251587的博客 107

OPTI Toolbox v2.28 安装与 3 个求解器补全:SCIP、SeDuMi、MOSEK 配置详解

本文详细介绍了OPTI Toolbox v2.28在MATLAB环境下的完整安装指南,包括SCIP、SeDuMi和MOSEK三个关键求解器的配置与优化技巧。通过分步指导和实战案例,帮助用户高效搭建强大的优化工具包,提升混合整数规划、二次约束二次规划等复杂问题的求解效率。

weixin_29039773的博客 350

GhidrAssist:AI驱动的逆向工程助手实战指南

逆向工程是分析软件内部工作原理、理解二进制代码逻辑的关键技术,广泛应用于软件安全、恶意代码分析和漏洞挖掘等领域。其核心原理在于通过反编译、控制流与数据流分析等手段,将机器码还原为可理解的高级语言表示。传统逆向分析高度依赖人工经验,面对海量代码时效率低下且易出错。随着大语言模型(LLM)技术的发展,其强大的代码理解与推理能力为自动化逆向分析提供了新的可能。通过将LLM与成熟的逆向工程框架(如Ghidra)相结合,可以构建智能辅助系统,实现代码意图的自动解释、变量/函数的智能重命名以及潜在漏洞模式的辅助识别,从

weixin_42547335的博客 596

WinForm中使用反射将业务对象绑定到窗体或控件容器

    在WebForm中,可以使用反射将业务对象绑定到 ASP.NET 窗体控件。最近做Winform项目,也参考WebForm中的代码实现同样的功能。    Winform没有提供类似WebForm中的FindControl方法,我于是用遍历控件的方式,写了一个类似WebForm中的这个方法,考虑到Winform中的很多控件放在Label、TabControl中,方法采用了递归的方式。   

3628

Langchain与Milvus集成中的pymilvus.exceptions.MilvusException: channel not found错误解析

本文深入解析了在Langchain与Milvus集成时常见的pymilvus.exceptions.MilvusException: channel not found错误。该错误通常由客户端配置错误、网络问题、服务端状态异常或版本不兼容导致。文章提供了从基础连通性测试到高级网络排查的完整故障排除指南,并针对本地Docker、远程服务器及Kubernetes等典型部署场景给出了具体解决方案,帮助开发者快速定位并修复这一连接层问题。

weixin_29229989的博客 217

ASP.NET利用反射进行物体与控件之间绑定

文单来自:http://bbs.blueidea.com/thread-2707666-1-1.html

u014230987的专栏 572

Deepfake检测模型-PyDeepFakeDet项目复现实验报告-深度学习

该实验是对于https://github.com/wangjk666/PyDeepFakeDet模型的复现。PyDeepFakeDet 是一款集成的、可扩展的 Deepfake 检测工具。目标是提供最先进的 Deepfake 检测模型以及用于在常用 Deepfake 数据集上训练和评估新模型的接口。

一位不是很硕的鉴伪方向硕士 5494

2025年7月5日全国青少年信息素养大赛图形化(Scratch)编程小学低年级组复赛真题+答案

2025年7月5日全国青少年信息素养大赛图形化(Scratch)编程小学低年级组复赛真题+答案

伶俐角少儿编程 898

使用反射将业务对象绑定到 ASP.NET 窗体控件

使用反射将业务对象绑定到 ASP.NET 窗体控件发布日期: 12/10/2004 | 更新日期: 12/10/2004John DyerDallas Theological Seminary适用于:Microsoft Visual Studio 2005 及早期版本ASP.NET 1.1C# 编程语言Visual Basic 编程语言摘要:使用反射以单行代码将业务对象绑定到

皮建华的专栏 1320

字符串尾部空格的迷思:深入解析 ABAP 隐式转换规则与 `strlen` 意外结果

看似简单的strlen之谜,背后实则是 ABAP 隐式转换的一整套严谨规则。只要开发者在赋值、输出、接口映射等关键节点建立“尾部空格意识”,辅以CONVCONDENSE等显式手段,就能有效杜绝多余空格引发的链式故障。希望本文的拆解与案例,能让你在面对下一个“异常长度”时,从容而笃定地给出正确答案。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 431

使用反射将业务对象绑定到 ASP.NET 窗体控件 (转)

使用反射将业务对象绑定到 ASP.NET 窗体控件发布日期: 12/10/2004 | 更新日期: 12/10/2004John DyerDallas Theological Seminary适用于:Microsoft Visual Studio 2005 及早期版本ASP.NET 1.1C# 编程语言Visual Basic 编程语言摘要:使用反射以单

盗版陈小春 1228

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

asp.net反射

using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.W

huyou1983218的专栏 1576

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

上一篇: Wap页面使用asp.net中移动控件List分页(原创)
下一篇: 如何更改服务器身份验证模式
patrickpan
博客等级 码龄21年 559粉丝 37原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值