一句代码实现批量数据绑定 上

对于一个以数据处理为主的应用中的UI层,我们往往需要编写相当多的代码去实现数据绑定。如果界面上的控件和作为数据源的实体类型之间存储某种约定的映射关系,我们就可以实现批量的数据绑定。为了验证这种想法,我写了一个小小的组件。这个小玩意仅仅是我花了两个小时写的,其中还有很多问题没有解决,比如对于空值的处理,特殊控件属性值的HTML编码问题,以及频繁反射的性能问题,仅仅演示一种解决思路而已。本篇着重介绍如何通过这个组件来解决我们在进行数据绑定过程中的常见问题,下篇会介绍它的设计。

目录:

  1. 基于控件ID/实体属性名映射的数据绑定
  2. 一句代码实现批量数据绑定
  3. 修正绑定数据的显示格式
  4. 过滤不需要绑定的属性
  5. 多个控件对应同一个实体属性

#p#

一、基于控件ID/实体属性名映射的数据绑定

我的这个组件暂时命名为DataBinder好了(注意和System.Web.UI.DataBinder区分),我们用它来将一个实体对象绑定给指定的容器控件中的所有子控件。下面是DataBinder的定义,两个BindData方法实现具体的绑定操作。

  1. public class DataBinder     
  2.      
  3.     public event EventHandler<DataBindingEventArgs> DataItemBinding;     
  4.     public event EventHandler<DataBindingEventArgs> DataItemBound;     
  5.       
  6.     public static IEnumerable<BindingMapping> BuildBindingMappings(Type entityType, Control container, string suffix = "");             
  7.     public void BindData(object entity, Control container, string suffix = "");     
  8.     public void BindData( object entity,IEnumerable<BindingMapping> bindingMappings);    

本文开头所说,自动批量的数据绑定依赖于控件和作为数据源实体类型的映射关系。在这里,我直接采用控件ID和实体属性名之间的映射。也就是说,在对于界面上控件进行命名的时候,应该根据对应的实体类型属性名进行规范命名。

另一方面,作为数据源的对象来说,它的所有属性并不都是为数据绑定而涉及。为了让DataBinder能够自动筛选用于绑定的属性,我在相应的属性上应用了一个自定义特性:DataPropertyAttribute。比如,下面的Customer对象会在后续的演示中用到,它的每一个数据属性都应用了这样一个DataPropertyAttribute特性。

  1. public class Customer     
  2. {     
  3.     [DataProperty]     
  4.     public string ID { get; set; }     
  5.     [DataProperty]     
  6.     public string FirstName { get; set; }     
  7.     [DataProperty]     
  8.     public string LastName { get; set; }     
  9.     [DataProperty]    
  10.     public string Gender { get; set; }    
  11.     [DataProperty]    
  12.     public int? Age { get; set; }    
  13.     [DataProperty]    
  14.     public DateTime? BirthDay { get; set; }    
  15.     [DataProperty]    
  16.     public bool? IsVip { get; set; }    

#p#

二、一句代码实现批量数据绑定

现在我们就来演示如何通过我们定义的DataBinder实现“一句代码的数据批量绑定”,而作为数据源就是我们上面定义的Customer对象。我们先来设计我们的页面,下面是主体部分的HTML,这是一个表格。需要注意的是:所有需要绑定到Customer对象的空间都和对应的属性具有相同的ID。

  1. <table>     
  2.  <tr>     
  3.      <td style="width:20%;text-align:right">ID:</td>     
  4.      <td><asp:Label ID="ID" runat="server"></asp:Label></td>     
  5.  </tr>     
  6.   <tr>     
  7.      <td style="width:20%;text-align:right">First Name:</td>     
  8.      <td><asp:TextBox ID="FirstName" runat="server"></asp:TextBox></td>     
  9.  </tr>    
  10.   <tr>    
  11.      <td style="width:20%;text-align:right">Last Name:</td>    
  12.      <td><asp:TextBox ID="LastName" runat="server"></asp:TextBox></td>    
  13.  </tr>    
  14.   <tr>    
  15.      <td style="width:20%;text-align:right">Gender:</td>    
  16.      <td>    
  17.          <asp:RadioButtonList ID="Gender" runat="server" RepeatDirection="Horizontal">    
  18.              <asp:ListItem Text="Male"   Value = "Male" />    
  19.              <asp:ListItem Text="Female" Value = "Female" />    
  20.          </asp:RadioButtonList>    
  21.      </td>    
  22.  </tr>    
  23.   <tr>    
  24.      <td style="width:20%;text-align:right">Age:</td>    
  25.      <td><asp:TextBox ID="Age" runat="server"></asp:TextBox></td>    
  26.  </tr>    
  27.   <tr>    
  28.      <td style="width:20%;text-align:right">Birthday:</td>    
  29.      <td><asp:TextBox ID="Birthday" runat="server" Width="313px"></asp:TextBox></td>    
  30.  </tr>    
  31.   <tr>    
  32.      <td style="width:20%;text-align:right">Is VIP:</td>    
  33.      <td><asp:CheckBox ID="IsVip" runat="server"></asp:CheckBox></td>    
  34.  </tr>    
  35.   <tr>     
  36.      <td colspan="2" align="center">    
  37.          <asp:Button ID="ButtonBind" runat="server" Text="Bind" onclick="ButtonBind_Click" />    
  38.      </td>    
  39.  </tr>    
  40. /table> 

为了编成方便,将DataBinder对象作为Page类型的一个属性,该属性在构造函数中初始化。

  1. public partial class Default : System.Web.UI.Page  
  2. {  
  3.     public Artech.DataBinding.DataBinder DataBinder { get; private set; }  
  4.     public Default()  
  5.     {  
  6.         this.DataBinder = new Artech.DataBinding.DataBinder();  
  7.     }  

然后我将数据绑定操作实现的Bind按照的Click事件中,对应所有的代码如下所示——真正的用于数据绑定的代码只有一句。

  1. protected void ButtonBind_Click(object sender, EventArgs e)  
  2. {  
  3.     var customer = new Customer  
  4.     {  
  5.         ID          = Guid.NewGuid().ToString(),  
  6.         FirstName   = "Zhang",  
  7.         LastName    = "San",  
  8.         Age         = 30,  
  9.         Gender      = "Male",  
  10.        BirthDay    = new DateTime(1981, 1, 1),  
  11.        IsVip       = true 
  12.    };  
  13.    this.DataBinder.BindData(customer, this);  

在浏览器中打开该Web页面,点击Bind按钮,你会发现绑定的数据已经正确显示在了对应的控件中:

 

#p#

三、修正绑定数据的显示格式

虽然通过DataBinder实现了对多个控件的批量绑定,但是并不***。一个显著的问题是:作为生日的字段不仅仅显示了日期,还显示了时间。我们如何让日期按照我们要求的格式进行显示呢?DataBinder为了提供了三种选择。

如果你注意看DataBinder定义了,你会发现它定义了两个事件:DataItemBinding和DataItemBound(命名有待商榷),它们分别在对某个控件进行绑定之前和之后触发。我们的***种方案就是注册DataItemBinding时间,为Birthday指定一个格式化字符串。假设我们需要的格式是“月-日-年”,那么我们指定的格式化字符串:MM-dd-yyyy。事件注册我方在了Page的构造函数中:

  1.  public Default()  
  2.  {  
  3.      this.DataBinder = new Artech.DataBinding.DataBinder();  
  4.      this.DataBinder.DataItemBinding += (sender, args) => 
  5.          {  
  6.              if (args.BindingMapping.Control == this.Birthday)  
  7.              {  
  8.                  args.BindingMapping.FormatString = "MM-dd-yyyy";  
  9.              }  
  10.         };  

运行程序,你会发现作为生日的字段已经按照我们希望的格式显示出来:

 

上面介绍了通过注册DataItemBinding事件在绑定前指定格式化字符串的解决方案,你也可以通过注册DataItemBound事件在绑定后修正显示的日期格式,相应的代码如下:

  1. public Default()  
  2. {  
  3.     this.DataBinder = new Artech.DataBinding.DataBinder();  
  4.     this.DataBinder.DataItemBound += (sender, args) => 
  5.         {  
  6.             if (args.BindingMapping.Control == this.Birthday && null != args.DataValue)  
  7.             {  
  8.                 this.Birthday.Text = ((DateTime)Convert.ChangeType(args.DataValue, typeof(DateTime))).  
  9.                     ToString("MM-dd-yyyy");  
  10.            }  
  11.        };  

DataBinder定义了两个BindData重载,我们使用的是通过指定数据源和容器控件的方式,而另一个重载的参数为IEnumerable<BindingMapping>类型。而BindingMapping是我们自定义的类型,用于表示控件和实体属性之间的运行时映射关系。而这样一个BindingMapping集合,可以通过DataBinder的静态方法BuildBindingMappings来创建。BindingMapping具有一个FormatString表示格式化字符串(实际上面我们指定的格式化字符串就是为这个属性指定的)。那么,我们也可以通过下面的代码来进行数据绑定:

  1. protected void ButtonBind_Click(object sender, EventArgs e)  
  2. {  
  3.     var customer = new Customer  
  4.     {  
  5.         ID          = Guid.NewGuid().ToString(),  
  6.         FirstName   = "Zhang",  
  7.         LastName    = "San",  
  8.         Age         = 30,  
  9.         Gender      = "Male",  
  10.        BirthDay    = new DateTime(1981, 1, 1),  
  11.        IsVip       = true 
  12.    };  
  13.    var bindingMappings = Artech.DataBinding.DataBinder.BuildBindingMappings(typeof(Customer), this);  
  14.    bindingMappings.Where(mapping => mapping.Control == this.Birthday).First().FormatString = "MM-dd-yyyy";  
  15.    this.DataBinder.BindData(customer, bindingMappings);  

#p#

四、过滤不需要绑定的属性

在默认的情况下,***个BindData方法(指定容器控件)会遍历实体的所有属性,将其绑定到对应的控件上。可能在有的时候,对于某些特殊的属性,我们不需要进行绑定。比如,某个控件的ID虽然符合实体属性的映射,但是它们表示的其实根本不是相同性质的数据。

为了解决在这个问题,在BindingMapping类型中定义了一个布尔类型的AutomaticBind属性。如果你在绑定前将该属性设置成False,那么基于该BindingMapping的数据绑定将被忽略。如果你调用BindData(object entity, Control container, string suffix = “”)这个重载,你可以通过注册DataItemBinding事件将相应BindingMapping的AutomaticBind属性设置成False。如果你调用BindData( object entity,IEnumerable<BindingMapping> bindingMappings)这个重载,你只需要在调用之间将相应BindingMapping的AutomaticBind属性设置成False。

我们将我们的程序还原成最初的状态,现在通过注册BindingMapping事件将基于Birthday的BindingMapping的AutomaticBind属性设置成False:

  1.  public Default()  
  2.  {  
  3.      this.DataBinder = new Artech.DataBinding.DataBinder();  
  4.      this.DataBinder.DataItemBinding += (sender, args) => 
  5.          {  
  6.              if (args.BindingMapping.Control == this.Birthday)  
  7.              {  
  8.                  args.BindingMapping.AutomaticBind = false;  
  9.              }  
  10.         };  

程序执行后,Birthday对应的TextBox将不会被绑定:

 

#p#

五、多个控件对应同一个实体属性

在上面的例子中,我们的控件的ID和对应的实体属性是相同的。但是在很多情况下,相同的页面上有不止一个控件映射到实体的同一个属性上。而控件ID的唯一性决定了我们不能为它们起相同的ID。在这种情况下,我们采用“基于后缀”的映射。也就是为,在为控件进行命名的时候,通过“实体属性名+后缀”形式来指定。

如果你仔细看了DataBinder的定义,不论是实例方法BindData(接受Control类型参数的),还是静态方法BuildBindingMappings,都具有一个缺省参数suffix,这就是为这种情况设计的。在默认的情况下,这个参数的值为空字符串,所以我们需要控件和实体属性具有相同的名称。如果控件是基于“实体属性名+后缀”来命名的,就需要显式指定这个参数了。为了演示这种情况,我们将例子中的所有需要绑定的空间ID加上一个“_Xyz”字符作为后缀。

  1. <table> 
  2.  <tr> 
  3.      <td style="width:20%;text-align:right">ID:</td> 
  4.      <td><asp:Label ID="ID_Xyz" runat="server"></asp:Label></td> 
  5.  </tr> 
  6.   <tr> 
  7.      <td style="width:20%;text-align:right">First Name:</td> 
  8.      <td><asp:TextBox ID="FirstName_Xyz" runat="server"></asp:TextBox></td> 
  9.  </tr> 
  10.  <tr> 
  11.     <td style="width:20%;text-align:right">Last Name:</td> 
  12.     <td><asp:TextBox ID="LastName_Xyz" runat="server"></asp:TextBox></td> 
  13. </tr> 
  14.  <tr> 
  15.     <td style="width:20%;text-align:right">Gender:</td> 
  16.     <td> 
  17.         <asp:RadioButtonList ID="Gender_Xyz" runat="server" RepeatDirection="Horizontal"> 
  18.             <asp:ListItem Text="Male"   Value = "Male" /> 
  19.             <asp:ListItem Text="Female" Value = "Female" /> 
  20.         </asp:RadioButtonList> 
  21.     </td> 
  22. </tr> 
  23. <tr> 
  24.     <td style="width:20%;text-align:right">Age:</td> 
  25.     <td><asp:TextBox ID="Age_Xyz" runat="server"></asp:TextBox></td> 
  26. </tr> 
  27.  <tr> 
  28.     <td style="width:20%;text-align:right">Birthday:</td> 
  29.     <td><asp:TextBox ID="Birthday_Xyz" runat="server" Width="313px"></asp:TextBox></td> 
  30. </tr> 
  31.  <tr> 
  32.     <td style="width:20%;text-align:right">Is VIP:</td> 
  33.     <td><asp:CheckBox ID="IsVip_Xyz" runat="server"></asp:CheckBox></td> 
  34. </tr> 
  35.  <tr> 
  36.     <td colspan="2" align="center"> 
  37.         <asp:Button ID="ButtonBind" runat="server" Text="Bind" onclick="ButtonBind_Click" /> 
  38.     </td> 
  39. </tr> 
  40. /table> 

如果采用指定容器控件进行直接绑定的话,就可以这样编程:

  1. protected void ButtonBind_Click(object sender, EventArgs e)  
  2. {  
  3.     var customer = new Customer  
  4.     {  
  5.         ID          = Guid.NewGuid().ToString(),  
  6.         FirstName   = "Zhang",  
  7.         LastName    = "San",  
  8.         Age         = 30,  
  9.         Gender      = "Male",  
  10.        BirthDay    = new DateTime(1981, 1, 1),  
  11.        IsVip       = true 
  12.    };  
  13.    this.DataBinder.BindData(customer, this, "_Xyz");  

如果通过预先创建的BindingMapping集合进行数据绑定,那么代码将是这样:

  1.  protected void ButtonBind_Click(object sender, EventArgs e)  
  2.  {  
  3.      var customer = new Customer  
  4.      {  
  5.          ID          = Guid.NewGuid().ToString(),  
  6.          FirstName   = "Zhang",  
  7.          LastName    = "San",  
  8.          Age         = 30,  
  9.          Gender      = "Male",  
  10.         BirthDay    = new DateTime(1981, 1, 1),  
  11.         IsVip       = true 
  12.     };  
  13.     
  14.     var bindingMappings = Artech.DataBinding.DataBinder.BuildBindingMappings(typeof(Customer), this, "_Xyz");  
  15.     this.DataBinder.BindData(customer, bindingMappings);  

 原文链接:http://www.cnblogs.com/artech/archive/2011/03/23/databinding.html

【编辑推荐】

  1. DBA应用技巧:如何升级InnoDB Plugin
  2. 十个节省时间的MySQL命令
  3. DBA必备:MySQL数据库常用操作和技巧
  4. MySQL日志操作教程:DBA们管理的利器
  5. MySQL触发器如何正确使用

 

文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/255918.html<

(0)
运维的头像运维
上一篇2025-05-01 01:52
下一篇 2025-05-01 01:54

相关推荐

  • 个人主题怎么制作?

    制作个人主题是一个将个人风格、兴趣或专业领域转化为视觉化或结构化内容的过程,无论是用于个人博客、作品集、社交媒体账号还是品牌形象,核心都是围绕“个人特色”展开,以下从定位、内容规划、视觉设计、技术实现四个维度,详细拆解制作个人主题的完整流程,明确主题定位:找到个人特色的核心主题定位是所有工作的起点,需要先回答……

    2025-11-20
    0
  • 社群营销管理关键是什么?

    社群营销的核心在于通过建立有温度、有价值、有归属感的社群,实现用户留存、转化和品牌传播,其管理需贯穿“目标定位-内容运营-用户互动-数据驱动-风险控制”全流程,以下从五个维度展开详细说明:明确社群定位与目标社群管理的首要任务是精准定位,需明确社群的核心价值(如行业交流、产品使用指导、兴趣分享等)、目标用户画像……

    2025-11-20
    0
  • 香港公司网站备案需要什么材料?

    香港公司进行网站备案是一个涉及多部门协调、流程相对严谨的过程,尤其需兼顾中国内地与香港两地的监管要求,由于香港公司注册地与中国内地不同,其网站若主要服务内地用户或使用内地服务器,需根据服务器位置、网站内容性质等,选择对应的备案路径(如工信部ICP备案或公安备案),以下从备案主体资格、流程步骤、材料准备、注意事项……

    2025-11-20
    0
  • 如何企业上云推广

    企业上云已成为数字化转型的核心战略,但推广过程中需结合行业特性、企业痛点与市场需求,构建系统性、多维度的推广体系,以下从市场定位、策略设计、执行落地及效果优化四个维度,详细拆解企业上云推广的实践路径,精准定位:明确目标企业与核心价值企业上云并非“一刀切”的方案,需先锁定目标客户群体,提炼差异化价值主张,客户分层……

    2025-11-20
    0
  • PS设计搜索框的实用技巧有哪些?

    在PS中设计一个美观且功能性的搜索框需要结合创意构思、视觉设计和用户体验考量,以下从设计思路、制作步骤、细节优化及交互预览等方面详细说明,帮助打造符合需求的搜索框,设计前的规划明确使用场景:根据网站或APP的整体风格确定搜索框的调性,例如极简风适合细线条和纯色,科技感适合渐变和发光效果,电商类则可能需要突出搜索……

    2025-11-20
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注