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) {
关于网站 | 客服中心 | 服务条款 | 友情链接 | 广告联系 | 本站历程 | 网站导航
吉ICP备05000107号