Monday 4 May 2015

What is the difference between <%#Eval%> or <%#Bind%> In a gridview

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

EVal is one way binding, Bind is two way
If you bind a value using Eval, it is like a read only. You can only view the data.
If you bind a value using Bind, and if you do some change on the value
it will reflect on the database also



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>