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

LIGGGHTS颗粒模拟入门:从DEM原理到工程实践 离散元法(DEM)是一种用于模拟颗粒物质行为的数值方法,其核心原理是通过计算每个颗粒的运动和相互作用来预测颗粒系统的宏观行为。该方法基于牛顿第二定律和接触力学模型,能够精确描述颗粒间的碰撞、摩擦和粘结等微观机制。在工程实践中,DEM技术为优化工业设备设计、预测材料流动和减少实验成本提供了重要价值。它广泛应用于制药造粒、矿石破碎、粉体输送和农业加工等领域,帮助工程师和研究人员深入理解颗粒系统的复杂动力学。LIGGGHTS作为一款基于LAMMPS的开源DEM软件,继承了其高效的并行计算架构,并针对颗粒模拟进行了 阅读详情

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

    }
}
 

从零搭建AGV避障系统:我用激光雷达+树莓派视觉模块踩过的那些坑 本文分享了基于树莓派与国产激光雷达从零搭建低成本AGV避障系统的实战经验。针对复杂仓库环境,详细剖析了激光雷达数据噪声、视觉模块算力瓶颈、IMU漂移等核心挑战,并提供了多传感器融合、软件架构设计与避障决策的具体解决方案,为中小型自动化改造项目提供了宝贵参考。 阅读详情

相关推荐

Wireshark实战解析IEC 61850 GOOSE报文:智能变电站通信调试指南

网络协议分析是理解现代工业通信系统的基础,其核心原理在于对数据包的捕获、解码与解析。在电力自动化领域,IEC 61850标准定义了智能变电站内设备间高效、可靠的通信框架,其中GOOSE(通用面向对象变电站事件)协议因其基于以太网多播和快速重传机制,成为保护跳闸、联锁等关键信号传输的基石,其技术价值在于实现了微秒级延迟的高可靠性通信。掌握GOOSE报文的分析能力,对于现场调试、故障定位及系统验证至关重要。本文聚焦于使用Wireshark这一网络分析工具,深入解析GOOSE报文结构,涵盖从以太网帧头、协议标识到

weixin_34401479的博客 380

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

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

weixin_33720452的博客 508

新华DCS系统仿真测试方法详细介绍

新华DCS ICAN3.1系统是应用广泛并且功能非常强大的国产DCS系统。具有管理虚拟CPU并仿真运行的功能。并且上位机画面可以与虚拟DPU数据实时同步仿真。具体仿真方法如下所示: 1、 需要设备及软件: 2、步骤如下: 2.1硬件步骤: 1)电脑开机。 2)交换机连接电脑网卡。 3)交换机上电运行。 2.2软件步骤: 1)设置电脑IP 一般配置IP为A网工程站IP。 222.222.221.201/255.255.255.0 2)建立虚拟DPU 登录 D:\iCAN\Program\Vxcu 文件夹。

lw_waston的博客 6919

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

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

weixin_30251587的博客 107

如何在 Windows 11 上安装 ChatGPT:CMD + 图形界面两种方式

我没说在 Windows 11 上装 ChatGPT 应用有多难,但你要是知道有哪几种官方途径可用,事情就会顺一点,尤其是对新手。对大多数用户来说,商店应用就是正确的选择。如果你在受管理的设备上,或者想避开商店,那么通过 Edge 或 Chrome 装 PWA 是完全站得住脚的替代方案。不管你选哪条路,真正的收获都是把 ChatGPT 从浏览器标签页里挪出来,放进一个它真正像个工具的地方。把它固定到任务栏、配好 Alt + 空格,你就会纳闷以前没它是怎么干活的。

山岚的运维笔记 345

【WorkBuddy从入门到精通实战教程】使用手册 第 3 章 WorkBuddy 的主界面、任务与工作区

WorkBuddy工作界面分为三个功能区:左侧任务管理区(新建/切换任务)、中间对话区(描述需求)、右侧结果区(查看产出)。系统通过隔离工作目录确保任务安全,推荐为每个任务创建独立目录。提供三种工作模式:Ask模式(仅查看)、Craft模式(直接操作文件)和Plan模式(多步骤确认执行),支持根据任务类型选择AI模型,不同模型在文本处理、图像识别等场景有针对性优化。关键功能包括任务隔离、权限控制和多模式协作,兼顾效率与安全性。

企鹅码农的专属播客,分享10多年的实战案例和经验解读,包含YOLO、RT-DETR、AI等各个工作领域。 360

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

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

3629

zabbix深度监控之邮件告警、微信群和微信告警

本文详细介绍了Zabbix监控系统实现邮件告警和微信告警的配置方法。邮件告警部分包含QQ和163邮箱的SMTP服务器配置、消息模板设置、告警流程三步骤(媒介配置、触发器动作、用户设置)。微信告警分为两种方式:企业微信群机器人告警(通过webhook实现)和企业微信应用告警(需创建应用并获取API参数)。两种告警方式都提供了完整的配置流程,包括脚本编写、权限设置、消息模板定制等关键步骤,并特别说明了如何使个人微信接收企业微信告警消息。文章还对比了SMTP、POP3、IMAP三种邮件协议的差异及适用场景。

提供C#相关开发 、云计算、运维测试、网络安全相关技术分享与服务,有意向可联系。 828

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

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

u014230987的专栏 572

五步通关!宝塔Linux部署ASP.NET Core的魔法秘籍大公开!

摘要: 本文提供ASP.NET Core应用五步部署攻略,涵盖从环境配置到线上运行的完整流程: 1️⃣ 环境准备:安装宝塔面板、ASP.NET Core运行时及开放端口; 2️⃣ 应用发布:通过dotnet publish生成独立部署包并上传服务器; 3️⃣ Nginx配置:设置反向代理将域名请求转发至应用端口; 4️⃣ 进程守护:使用Supervisor确保应用持续运行; 5️⃣ HTTPS加密:通过宝塔快速申请SSL证书并解决验证路径冲突。 关键点: 强调权限设置(755)、端口开放及反向代理细节; 提

java专栏 974

ConvNeXt 改进 :ConvNeXt添加GnConv递归门控卷积,二次创新CNBlock结构 ,独家首发

视觉 Transformer 在多种任务中取得了显著的成功,这得益于基于点积自注意力的新空间建模机制。视觉 Transformer 中的关键因素——即输入自适应、长距离和高阶空间交互——也可以通过卷积框架高效实现。作者提出了递归门控卷积(Recursive Gated Convolution,简称 gnConv),通过门控卷积和递归设计执行高阶空间交互。该新操作具有高度的灵活性和可定制性,兼容各种卷积变体,并将自注意力中的二阶交互扩展到任意阶数,而不会引入显著的额外计算。

在职AI算法工程师,擅长计算机视觉,YOLO目标检测、分割等,擅长web、pyqt界面可视化,好内容持续更新中,来这里跟大家一起学习,共同进步 246

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

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

皮建华的专栏 1320

如何正确的计算两个角度的角度差?

这一步是必要的,因为它确保了无论a相对于b是在逆时针方向还是顺时针方向,d2总是正确地反映出绕圆相反方向的角度差。这样,当函数进行最后的比较时,它能够准确地比较两个方向的旋转路径并选择最短的一个。

小秋 AI SLAM 入门实战教程 4147

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

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

盗版陈小春 1229

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

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的专栏 1580

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

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

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址