分享
 
 
 

Digging Into Data Binding Expressions

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

source:

http://odetocode.com/Articles/278.aspx

begin:

Posted by scott on 2004年10月23日

This article will demonstrate some techniques beyond simple DataBinder.Eval calls in ASP.NET data binding expressions.

Data binding expressions in ASP.NET are the small snippets of code you see between <%# and %> characters in an ASPX file. We normally see these expressions in a Repeater’s ItemTemplate declarations, and in a DataGrid's TemplateColumn markup. Also, Data binding expressions often contain a call to the DataBinder.Eval method. Although these are the common scenarios for data binding in ASP.NET, there is more you can do with a little knowledge of what happens underneath the covers. In this article we will take a look at how the data binding expressions work, where they work, and demonstrate some additional tricks you can use to get more from your data binding expressions.

As a refresher, let’s look at a simple webform with data binding expressions:

<form id="Form1" method="post" runat="server">

<table>

<asp:Repeater id="Repeater1" runat="server">

<ItemTemplate>

<tr>

<td><%# DataBinder.Eval(Container.DataItem, "Name") %></td>

<td><%# DataBinder.Eval(Container.DataItem, "HexValue") %></td>

</tr>

</ItemTemplate>

</asp:Repeater>

</table>

</form>

This produces the web form shown below.

In this example we have a Repeater control displaying color names and their hexadecimal equivalents. We know we can bind a Repeater to DataTable objects, DataView objects, SqlDataReader objects, and others. One of the nice features of data binding is how ASP.NET abstracts away the ultimate source of the data and we don’t need to know the exact type of the object we are binding against.

Our first data binding tip is that you can also bind against a collection of custom objects. For instance, in our sample web form we are using an ArrayList of Color classes, where the Color class is our own custom class, defined below.

public class Color

{

public Color(string name, byte r, byte g, byte b)

{

this.name = name;

hexValue = String.Format(

"#{0:X2}{1:X2}{2:X2}",

r, g, b

);

}

public string Name

{

get { return name; }

}

public string HexValue

{

get { return hexValue; }

}

private string name;

private string hexValue;

}

The Color class constructor takes a name, and the red, green, and blue values to describe the color. We can build an ArrayList of the class using the following code.

public static ArrayList GetColors()

{

ArrayList list = new ArrayList();

list.Add(new Color(System.Drawing.Color.AliceBlue.Name,

System.Drawing.Color.AliceBlue.R,

System.Drawing.Color.AliceBlue.G,

System.Drawing.Color.AliceBlue.B)

);

list.Add(new Color(System.Drawing.Color.Beige.Name,

System.Drawing.Color.Beige.R,

System.Drawing.Color.Beige.G,

System.Drawing.Color.Beige.B)

);

list.Add(new Color(System.Drawing.Color.Chocolate.Name,

System.Drawing.Color.Chocolate.R,

System.Drawing.Color.Chocolate.G,

System.Drawing.Color.Chocolate.B)

);

list.Add(new Color(System.Drawing.Color.DarkMagenta.Name,

System.Drawing.Color.DarkMagenta.R,

System.Drawing.Color.DarkMagenta.G,

System.Drawing.Color.DarkMagenta.B)

);

list.Add(new Color(System.Drawing.Color.Fuchsia.Name,

System.Drawing.Color.Fuchsia.R,

System.Drawing.Color.Fuchsia.G,

System.Drawing.Color.Fuchsia.B)

);

list.Add(new Color(System.Drawing.Color.PapayaWhip.Name,

System.Drawing.Color.PapayaWhip.R,

System.Drawing.Color.PapayaWhip.G,

System.Drawing.Color.PapayaWhip.B)

);

list.Add(new Color(System.Drawing.Color.Violet.Name,

System.Drawing.Color.Violet.R,

System.Drawing.Color.Violet.G,

System.Drawing.Color.Violet.B

)

);

list.Add(new Color(System.Drawing.Color.Black.Name,

System.Drawing.Color.Black.R,

System.Drawing.Color.Black.G,

System.Drawing.Color.Black.B

)

);

return list;

}

Displaying values inside of <td> tags is not the only place to use a data binding expression. You can also use data binding to change the appearance of a control. In the next sample we will set the background color of a row using data binding.

<asp:Repeater id="Repeater1" runat="server">

<ItemTemplate>

<tr bgcolor="<%# DataBinder.Eval(Container.DataItem, "HexValue")%>">

<td><%# DataBinder.Eval(Container.DataItem, "Name") %></td>

<td><%# DataBinder.Eval(Container.DataItem, "HexValue") %></td>

</tr>

</ItemTemplate>

</asp:Repeater>

This gives us the following form.

In the next section we will dig into see how data binding happens at runtime.

Under The Covers Of The Data Binding Expression

In order to really understand what happens inside of the data binding expression, let’s take a look at the code the runtime generates for the ASPX file.

public void __DataBind__control3(object sender, System.EventArgs e) {

System.Web.UI.WebControls.RepeaterItem Container;

System.Web.UI.DataBoundLiteralControl target;

target = ((System.Web.UI.DataBoundLiteralControl)(sender));

Container = ((System.Web.UI.WebControls.RepeaterItem)(target.BindingContainer));

#line 17 "E:\dev\xprmnt\aspnet\DataBinding\SimpleData.aspx"

target.SetDataBoundString(0,

System.Convert.ToString(DataBinder.Eval(Container.DataItem, "HexValue")));

#line 18 "E:\dev\xprmnt\aspnet\DataBinding\SimpleData.aspx"

target.SetDataBoundString(1,

System.Convert.ToString(DataBinder.Eval(Container.DataItem, "Name")));

#line 19 "E:\dev\xprmnt\aspnet\DataBinding\SimpleData.aspx"

target.SetDataBoundString(2,

System.Convert.ToString(DataBinder.Eval(Container.DataItem, "HexValue")));

}

The above is an excerpt from the code generated for our web form into the temporary ASP.NET files directory. It shows us how the Container variable that we use in the call to Eval is set to reference a BindingContainer setup by the runtime. This method, an event handler, will fire for each item the control needs to bind (once for each row, or once for each list item in this example).

The most important point to take from the above code is how the expression we use for data binding is placed as a parameter to Convert.ToString. This means we can use any expression that will yield a string. For example, the following ItemTemplate would produce the same screen we saw in the last screenshot.

<asp:Repeater id="Repeater1" runat="server">

<ItemTemplate>

<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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有