Tuesday, 27 May 2014

Difference Between Eval() and Bind() in Asp.Net

In this article, i am going to explain difference between Eval() and Bind(). The main difference between the two is 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. See implementation below:-

Example 1 :- We can do this
      <asp:TemplateField HeaderText="Price">
                  <ItemTemplate>
                      <%# Eval("price") %>
                  </ItemTemplate>
      </asp:TemplateField>

Example 2 :- We can't do this
      <asp:TemplateField HeaderText="Price">
                  <ItemTemplate>
                      <%# Bind("price") %>
                  </ItemTemplate>
      </asp:TemplateField>
Output of Example 2
Example 3 :- We can do this
  <asp:TemplateField HeaderText="Price">
       <ItemTemplate>
            <asp:Label ID="lblPrice" runat="server" Text='<%# Bind("price") %>'></asp:Label>
       </ItemTemplate>
  </asp:TemplateField>
The difference between Example 2 and Example 3 was nothing expect one server side control. As i already told you, Bind must be assigned to a property of server side controls, that's why Example 3 is perfect but Example 2 not.


That’s it!!…..Happy Programming...

No comments:

Post a Comment