转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 电脑应用 >> 电脑技术 >> 正文
Digging Into Data Binding Expressions         ★★★

Digging Into Data Binding Expressions

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1726 更新时间:2006/7/27
ate> <tr bgcolor="<%# ((Color)Container.DataItem).HexValue %>"> <td><%# ((Color)Container.DataItem).Name %></td> <td><%# ((Color)Container.DataItem).HexValue %></td> </tr> </ItemTemplate> </asp:Repeater>

In this case we are skipping a call to DataBind.Eval and casting the DataItem to our Color type (note, in order to do this, you’ll need to import the namespace used where Color is declared with the <@ Import > directive, i.e. <%@ Import Namespace="aspnet.DataBinding" %> if the Color type is in a class file under a namespace of aspnet.DataBinding.

Using the DataBinder.Eval technique allows us to avoid putting messy casts into the ASPX. Instead of casts, Eval uses reflection techniques to dynamically find a property by name at runtime. Because reflection is inherently slow, Eval is relatively slower than using a cast. On the other hand, one advantage to DataBinder.Eval is that the syntax will work in an ASPX compiled for either C# or VB.NET. In the form shown above the casting syntax will only work if the ASPX page compiles with the C# compiler.

While the above example demonstrates how we do not necessarily need to use DataBinder.Eval in our data binding expressions, let’s take the concept one step further and call a method in our code-behind class from the data binding expression.

<asp:Repeater id="Repeater1" runat="server">
  <ItemTemplate>
    <tr bgcolor="<%# ((Color)Container.DataItem).HexValue %>">
      <td><%# GetColorName(Container.DataItem)  %></td>
      <td><%# ((Color)Container.DataItem).HexValue %></td>
    </tr>
  </ItemTemplate>
</asp:Repeater>

In the above template we call the GetColorName method and pass the DataItem as a parameter. Calling a method inside of a data binding expression allows us to use additional logic in the compiled, intellisensed environment of our code behind class. The method needs to be declared as a protected method of the code-behind class in order for this to work, because the class dynamically generated from the ASPX markup is derived from the class defined in the code behind file. The method in our code behind is shown below.

protected string GetColorName(object o)
{
   string name = string.Empty;

   Color color = o as Color;
   if(color != null)
   {
      name = color.Name;
      int i = 1;
      do
      {
         if(char.IsUpper(name, i))
         {
            name = name.Insert(i, " ");
            i = i +2;
         }
         else
         {
            i = i + 1;
         }

      } while(i < name.Length);
   }

   return name;
}

The method above will take the name of the color “PapayaWhip” and insert spaces in front of all uppercase letters after the first letter, yielding “Papaya Whip”.

Hopefully this article has demonstrated some additional techniques you can use in data binding scenarios. Don’t limit your self to DataBinder.Eval if the scenario calls for some additional formatting logic!

By Scott Allen

上一页  [1] [2] 


[办公软件]GETPIVOTDATA函数语法介绍  [常用软件]桌面搜索新利器 “88Data”
[VB.NET程序]VB.NET Data Types  [Delphi程序]Create a menu item into the Delphi menu
[VB.NET程序]如何用VB实现QBASIC中的data数据的read  [Web开发]Binding a DataGrid to an ADO Recordset
[Web开发]ADO在MICROSOFT DATA ACCESS 中的角色  [Web开发]ADO Data Types与数据库值对应
[Web开发]Handling Data Concurrency Using ADO.NET  [Web开发]讲一讲ASP处理数据用的组件ADO (ActiveX Data Obj…
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · 电脑技术  · 操作系统
    · 磁盘工具  · 视音频技术
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台