Difference between Eval and Bind - Data Binding Expressions in ASP.NET

In simple words, the Eval function is used to define one-way binding (read only). The Bind function is used for two-way binding (read-write)

So in a control like the GridView or the FormView, when you have to display read only data using a label control, you would be using the Eval function whereas when you want to update a database field, you would be using a TextBox and thus using the Bind function.

Note: You can use the Bind function to retrieve the value of a databound field, just as Eval does.

Here’s an example:

<asp:FormView ID="FormView1" runat="server"
AllowPaging="True"
DataKeyNames="CustomerID"
DataSourceID="SqlDataSource1">
<
EditItemTemplate>
CustomerName:
<asp:TextBox ID="txtCustName"
runat="server"
Text='<%# Bind("CName") %>'>
</
asp:TextBox>
</
EditItemTemplate>

<
ItemTemplate>
CustID:
<asp:Label ID="lblCustID"
runat="server"
Text='<%# Eval("CID") %>'>
</
asp:Label>
</
ItemTemplate>
</
asp:FormView>
You can read more on the Eval and Bind functions

Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

No comments: