What's the difference between <#eval and <#bind in asp.net

Viewed 35088

In a gridview, we can use <%#Eval%> or <%#Bind%> to output values from a database. What is the difference between them?

7 Answers

as they said Eval is one way and Bind is two way but one more important difference Bind must be assigned to a property of server side control (runat="server") while you can assign Eval to server side or client side control

<asp:ListView ID="listview1" runat="server">
    <ItemTemplate>
         <%--you can do this--%>
         <asp:Label ID="label1" runat="server" Text="<%#Bind('xx')  %>"></asp:Label>
         <%--you can do this--%>
         <asp:Label ID="label2" runat="server" Text="<%#Eval('xx')  %>"></asp:Label>
         <div>
         <%--WILL CAUSE AN ERROR--%>
             "<%#Bind('xx')  %>" 
         <%--you can do this--%>
             "<%#Eval('xx')  %>" 
         </div>
    </ItemTemplate>
</asp:ListView>

Eval and Bind functions are used to bind data from database to controls present inside DataBound controls like GridView, DetailsView, Repeater, DataList, etc.

The difference between Eval and Bind is that Eval function is used to bind data to control inside a DataBound control, but it cannot update the values back to the database.

On the other hand, Bind function can be used to bind data to control inside a DataBound control and also it can update the values back to the database.

Related