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




About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

No comments: