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…
|