Saturday, March 24, 2012

Simple Data Binding Question

What is the difference in using Bind and Eval when doing declaratvie data binding?

Explanation

In a GridView when I create a Bound Column and them template it I end up with:

<asp:Label ID="Label11" runat="server" Text='<%# Bind("CustomerSat") %>'></asp:Label>

The following also works:

<asp:Label ID="Label11" runat="server" Text='<%# Eval("CustomerSat") %>'></asp:Label>

The following does not work - get a compiler error:

<asp:Label ID="Label11" runat="server" Text='<%# GetCustomerSat (Bind("CustomerSat")) %>'></asp:Label>

The following does work:

<asp:Label ID="Label11" runat="server" Text='<%# GetCustomerSat (Eval("CustomerSat")) %>'></asp:Label>

Of course the old way works:

<asp:Label ID="Label11" runat="server" Text='<%# GetCustomerSat (DataBinder.Eval(Container, "DataItem.CustomerSat")) %>'></asp:Label>

Bind only allows for one-way data binding. That means that you can only return the value, but not manipulate it. If you need to pass it to a function or method, you need to use Eval().

HTH,
Ryan

0 comments:

Post a Comment