Difference Between Eval And Bind Functions In ASP.NET

Difference between Eval and Bind function in ASP.Net

 
Eval function
Bind function
1. The Eval function is used to define one-way binding (read only), e.g., 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.
The Bind function is used for two-way binding (read-write), e.g., when you want to update a database field, you would be using a Textbox and thus using the Bind function.
2. Eval function is used to bind data to control inside a DataBound control, but it cannot update the values back to the database.
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.
3. Eval is a protected method defined on the Template Control class, from which the Page class is derived.
Bind is a new ASP.NET 2.0 databinding keyword. It's not a method of any specific class.
4. It does not need the ID of a particular control
e.g.,
<table>
<tr>
<td><%#Eval("productname")%> </td>
</tr>
</table>
is OK.
It always needs the ID of a particular control
e.g.,
<table>
<tr>
<td><%#Bind("productname")%> </td>
</tr>
</table>
is WRONG.
We have to take some UI control like Label and specify the Binding in the Text property.
e.g.,
<ItemTemplate>
<asp:Label ID="lblDOB" runat="server" Text='<%# Bind("DOB") %>'></asp:Label>
</ItemTemplate>
5. It applies to controls like the GridView, DataList, Repeater, ListView, FormsView, DetailsView.
It applies to controls like the GridView, FormsView, and DetailsView.
 

The Eval function

 
The following HTML Markup consists of an ASP.Net GridView consisting of TextBoxes inside TemplateField column of GridView and I have created a procedure to fetch values from database using stored procedure.
 
I have created a table and inserted values into it.
  1. SET ANSI_NULLS ON  
  2. GO  
  3. SET QUOTED_IDENTIFIER ON  
  4. GO  
  5. SET ANSI_PADDING ON  
  6. GO  
  7. CREATE TABLE [dbo].[Customer](  
  8.     [CustomerID] [int] IDENTITY(1,1) NOT NULL,  
  9.     [CustomerName] [varchar](50) NULL,  
  10.     [ContactName] [varchar](50) NULL,  
  11.     [Address1] [varchar](100) NULL,  
  12.     [City] [varchar](50) NULL,  
  13.     [PostalCode] [varchar](50) NULL,  
  14.     [Country] [varchar](50) NULL,  
  15.     [salary] [intNULL  
  16. ON [PRIMARY]  
  17. GO  
  18. SET ANSI_PADDING OFF  
  19. GO  
  20. SET IDENTITY_INSERT [dbo].[Customer] ON  
  21. INSERT [dbo].[Customer] ([CustomerID], [CustomerName], [ContactName], [Address1], [City], [PostalCode], [Country], [salary]) VALUES (1, N'Alfreds Futterkiste', N'Maria Anders', N'Obere Str. 57', N'Berlin', N'12209', N'Germany', 60000)  
  22. INSERT [dbo].[Customer] ([CustomerID], [CustomerName], [ContactName], [Address1], [City], [PostalCode], [Country], [salary]) VALUES (2, N'Ana Trujillo Emparedados y helados', N'Ana Trujillo', N'Avda. de la Constitución 2222', N'México D.F.', N'05021', N'Mexico', 70000)  
  23. INSERT [dbo].[Customer] ([CustomerID], [CustomerName], [ContactName], [Address1], [City], [PostalCode], [Country], [salary]) VALUES (3, N'Antonio Moreno Taquería', N'Antonio Moreno', N'Mataderos 2312', N'México D.F.', N'05023', N'Mexico', 80000)  
  24. INSERT [dbo].[Customer] ([CustomerID], [CustomerName], [ContactName], [Address1], [City], [PostalCode], [Country], [salary]) VALUES (4, N'Around the Horn', N'Thomas Hardy', N'120 Hanover Sq.', N'London', N'WA1 1DP', N'UK', 90000)  
  25. INSERT [dbo].[Customer] ([CustomerID], [CustomerName], [ContactName], [Address1], [City], [PostalCode], [Country], [salary]) VALUES (5, N'Berglunds snabbköp', N'Christina Berglund', N'Berguvsvägen 8', N'Luleå', N'S-958 22', N'Sweden', 150000)  
  26. INSERT [dbo].[Customer] ([CustomerID], [CustomerName], [ContactName], [Address1], [City], [PostalCode], [Country], [salary]) VALUES (6, N'Alfreds Futterkiste', N'Maria Anders', N'Obere Str. 57', N'Berlin', N'12209', N'Germany', 1150000)  
  27. SET IDENTITY_INSERT [dbo].[Customer] OFF 
The [Customer] table,now look like this after data insertion,
 
Difference Between Eval And Bind Functions In ASP.NET
 
This is an best example of Eval function as here the main objective is to display data and no Insert or Update operation is required.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Eval_Bind.aspx.cs" Inherits="GRIDOPERATION.GRIDVIEW_PROJECT.Eval_Bind" %>    
  2.     
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">    
  4.     
  5. <html xmlns="http://www.w3.org/1999/xhtml">    
  6. <head runat="server">    
  7.     <title></title>    
  8. </head>    
  9. <body>    
  10.     <form id="form1" runat="server">    
  11.     <div>    
  12.         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">    
  13.         <Columns>    
  14.         <asp:TemplateField HeaderText="Name" ItemStyle-Width="80">    
  15.             <ItemTemplate>    
  16.                 <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("CustomerID") %>' />    
  17.             </ItemTemplate>    
  18.         </asp:TemplateField>    
  19.         <asp:TemplateField HeaderText="Country" ItemStyle-Width="100">    
  20.             <ItemTemplate>    
  21.                 <asp:TextBox ID="txtCountry" runat="server" Text='<%# Eval("CustomerName") %>' />    
  22.             </ItemTemplate>    
  23.         </asp:TemplateField>    
  24.         <asp:TemplateField HeaderText="Name" ItemStyle-Width="80">    
  25.             <ItemTemplate>    
  26.                 <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("ContactName") %>' />    
  27.             </ItemTemplate>    
  28.         </asp:TemplateField>    
  29.         <asp:TemplateField HeaderText="Country" ItemStyle-Width="100">    
  30.             <ItemTemplate>    
  31.                 <asp:TextBox ID="txtCountry" runat="server" Text='<%# Eval("Address1") %>' />    
  32.             </ItemTemplate>    
  33.         </asp:TemplateField>    
  34.         <asp:TemplateField HeaderText="Name" ItemStyle-Width="80">    
  35.             <ItemTemplate>    
  36.                 <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("City") %>' />    
  37.             </ItemTemplate>    
  38.         </asp:TemplateField>    
  39.         <asp:TemplateField HeaderText="Country" ItemStyle-Width="100">    
  40.             <ItemTemplate>    
  41.                 <asp:TextBox ID="txtCountry" runat="server" Text='<%# Eval("PostalCode") %>' />    
  42.             </ItemTemplate>    
  43.         </asp:TemplateField>    
  44.         <asp:TemplateField HeaderText="Name" ItemStyle-Width="80">    
  45.             <ItemTemplate>    
  46.                 <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Country") %>' />    
  47.             </ItemTemplate>    
  48.         </asp:TemplateField>    
  49.         <asp:TemplateField HeaderText="Country" ItemStyle-Width="100">    
  50.             <ItemTemplate>    
  51.                 <asp:TextBox ID="txtCountry" runat="server" Text='<%# Eval("salary") %>' />    
  52.             </ItemTemplate>    
  53.         </asp:TemplateField>    
  54.         </Columns>    
  55.         </asp:GridView>    
  56.     </div>    
  57.     </form>    
  58. </body>    
  59. </html>  <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Eval_Bind.aspx.cs" Inherits="GRIDOPERATION.GRIDVIEW_PROJECT.Eval_Bind" %>    
  60.     
  61. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">    
  62.     
  63. <html xmlns="http://www.w3.org/1999/xhtml">    
  64. <head runat="server">    
  65.     <title></title>    
  66. </head>    
  67. <body>    
  68.     <form id="form1" runat="server">    
  69.     <div>    
  70.         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">    
  71.         <Columns>    
  72.         <asp:TemplateField HeaderText="Name" ItemStyle-Width="80">    
  73.             <ItemTemplate>    
  74.                 <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("CustomerID") %>' />    
  75.             </ItemTemplate>    
  76.         </asp:TemplateField>    
  77.         <asp:TemplateField HeaderText="Country" ItemStyle-Width="100">    
  78.             <ItemTemplate>    
  79.                 <asp:TextBox ID="txtCountry" runat="server" Text='<%# Eval("CustomerName") %>' />    
  80.             </ItemTemplate>    
  81.         </asp:TemplateField>    
  82.         <asp:TemplateField HeaderText="Name" ItemStyle-Width="80">    
  83.             <ItemTemplate>    
  84.                 <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("ContactName") %>' />    
  85.             </ItemTemplate>    
  86.         </asp:TemplateField>    
  87.         <asp:TemplateField HeaderText="Country" ItemStyle-Width="100">    
  88.             <ItemTemplate>    
  89.                 <asp:TextBox ID="txtCountry" runat="server" Text='<%# Eval("Address1") %>' />    
  90.             </ItemTemplate>    
  91.         </asp:TemplateField>    
  92.         <asp:TemplateField HeaderText="Name" ItemStyle-Width="80">    
  93.             <ItemTemplate>    
  94.                 <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("City") %>' />    
  95.             </ItemTemplate>    
  96.         </asp:TemplateField>    
  97.         <asp:TemplateField HeaderText="Country" ItemStyle-Width="100">    
  98.             <ItemTemplate>    
  99.                 <asp:TextBox ID="txtCountry" runat="server" Text='<%# Eval("PostalCode") %>' />    
  100.             </ItemTemplate>    
  101.         </asp:TemplateField>    
  102.         <asp:TemplateField HeaderText="Name" ItemStyle-Width="80">    
  103.             <ItemTemplate>    
  104.                 <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Country") %>' />    
  105.             </ItemTemplate>    
  106.         </asp:TemplateField>    
  107.         <asp:TemplateField HeaderText="Country" ItemStyle-Width="100">    
  108.             <ItemTemplate>    
  109.                 <asp:TextBox ID="txtCountry" runat="server" Text='<%# Eval("salary") %>' />    
  110.             </ItemTemplate>    
  111.         </asp:TemplateField>    
  112.         </Columns>    
  113.         </asp:GridView>    
  114.     </div>    
  115.     </form>    
  116. </body>    
  117. </html>    
The code behind it is
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Web.Configuration;  
  8. using System.Data.SqlClient;  
  9. using System.Data;  
  10.   
  11. namespace GRIDOPERATION.GRIDVIEW_PROJECT  
  12. {  
  13.     public partial class Eval_Bind : System.Web.UI.Page  
  14.     {  
  15.         string strConnString = WebConfigurationManager.ConnectionStrings["ConDB"].ConnectionString;  
  16.   
  17.         protected void Page_Load(object sender, EventArgs e)  
  18.         {  
  19.             if (!this.IsPostBack)  
  20.             {  
  21.                 gedata();  
  22.             }  
  23.         }  
  24.         public void gedata()  
  25.         {  
  26.   
  27.             SqlConnection con = new SqlConnection(strConnString);  
  28.             SqlCommand cmd = new SqlCommand("[display_data]", con);  
  29.             cmd.CommandType = CommandType.StoredProcedure;  
  30.             con.Open();  
  31.             SqlDataAdapter sda = new SqlDataAdapter(cmd);  
  32.             DataSet ds = new DataSet();  
  33.             sda.Fill(ds);  
  34.             GridView1.DataSource = ds;  
  35.             GridView1.DataBind();  
  36.             con.Close();  
  37.   
  38.         }  
  39.     }  

Here I created a procedure named "[display_data]"
  1. /****** Object:  StoredProcedure [dbo].[display_data]    Script Date: 07/25/2019 10:14:21 ******/  
  2. SET ANSI_NULLS ON  
  3. GO  
  4. SET QUOTED_IDENTIFIER ON  
  5. GO  
  6. ALTER procedure [dbo].[display_data]  
  7.   
  8. as   
  9. begin  
  10. SELECT [CustomerID]  
  11.       ,[CustomerName]  
  12.       ,[ContactName]  
  13.       ,[Address1]  
  14.       ,[City]  
  15.       ,[PostalCode]  
  16.       ,[Country]  
  17.       ,[salary]  
  18.   FROM [Customer]  
  19. end   
The following screenshot displays the GridView consisting of TextBoxes whose values are populated using Eval function.

Difference Between Eval And Bind Functions In ASP.NET
 

The Bind function

 
In the below GridView, if you modify a TextBox value and click Update then the value will also be updated in the database.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Bind.aspx.cs" Inherits="GRIDOPERATION.GRIDVIEW_PROJECT.Bind" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head runat="server">  
  6.     <title></title>  
  7. </head>  
  8. <body>  
  9.     <form id="form1" runat="server">  
  10.     <div>  
  11.         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowUpdating="GridView1_RowUpdating" DataKeyNames="CustomerID" >  
  12.             <Columns>  
  13.                 <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" InsertVisible="False"  
  14.                     ReadOnly="True" SortExpression="CustomerID" />  
  15.                 <asp:TemplateField HeaderText="CustomerName" ItemStyle-Width="100">  
  16.                     <ItemTemplate>  
  17.                         <asp:TextBox ID="txtCustomerName" runat="server" Text='<%# Bind("CustomerName") %>' />  
  18.                     </ItemTemplate>  
  19.                 </asp:TemplateField>  
  20.                 <asp:TemplateField HeaderText="ContactName" ItemStyle-Width="80">  
  21.                     <ItemTemplate>  
  22.                         <asp:TextBox ID="txtContactName" runat="server" Text='<%# Bind("ContactName") %>' />  
  23.                     </ItemTemplate>  
  24.                 </asp:TemplateField>  
  25.                 <asp:TemplateField HeaderText="Address1" ItemStyle-Width="100">  
  26.                     <ItemTemplate>  
  27.                         <asp:TextBox ID="txtAddress1" runat="server" Text='<%# Bind("Address1") %>' />  
  28.                     </ItemTemplate>  
  29.                 </asp:TemplateField>  
  30.                 <asp:TemplateField HeaderText="City" ItemStyle-Width="80">  
  31.                     <ItemTemplate>  
  32.                         <asp:TextBox ID="txtCity" runat="server" Text='<%# Bind("City") %>' />  
  33.                     </ItemTemplate>  
  34.                 </asp:TemplateField>  
  35.                 <asp:TemplateField HeaderText="PostalCode" ItemStyle-Width="100">  
  36.                     <ItemTemplate>  
  37.                         <asp:TextBox ID="txtPostalCode" runat="server" Text='<%# Bind("PostalCode") %>' />  
  38.                     </ItemTemplate>  
  39.                 </asp:TemplateField>  
  40.                 <asp:TemplateField HeaderText="Country" ItemStyle-Width="80">  
  41.                     <ItemTemplate>  
  42.                         <asp:TextBox ID="txtCountry" runat="server" Text='<%# Bind("Country") %>' />  
  43.                     </ItemTemplate>  
  44.                 </asp:TemplateField>  
  45.                 <asp:TemplateField HeaderText="salary" ItemStyle-Width="100">  
  46.                     <ItemTemplate>  
  47.                         <asp:TextBox ID="txtsalary" runat="server" Text='<%# Bind("salary") %>' />  
  48.                     </ItemTemplate>  
  49.                 </asp:TemplateField>  
  50.                 <asp:TemplateField HeaderText="" ItemStyle-Width="100">  
  51.                     <ItemTemplate>  
  52.                   <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>   
  53.                     </ItemTemplate>  
  54.                 </asp:TemplateField>  
  55.             </Columns>  
  56.         </asp:GridView>  
  57.     </div>  
  58.     </form>  
  59. </body>  
  60. </html> 
The code behind it is
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Data.SqlClient;  
  9. using System.Configuration;  
  10.   
  11. namespace GRIDOPERATION.GRIDVIEW_PROJECT  
  12. {  
  13.     public partial class Bind : System.Web.UI.Page  
  14.     {  
  15.         SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConDB"].ToString());  
  16.         SqlDataAdapter sda = new SqlDataAdapter();  
  17.         SqlCommand cmd = new SqlCommand();  
  18.         DataTable dataTable;  
  19.         protected void Page_Load(object sender, EventArgs e)  
  20.         {  
  21.             if (!IsPostBack)  
  22.                 display();  
  23.         }  
  24.   
  25.         private void display()  
  26.         {  
  27.             dataTable = new DataTable();  
  28.             cmd.Connection = con;  
  29.             cmd.CommandText = "SELECT * FROM Customer";  
  30.             sda = new SqlDataAdapter(cmd);  
  31.             sda.Fill(dataTable);  
  32.             GridView1.DataSource = dataTable;  
  33.             GridView1.DataBind();  
  34.         }  
  35.         protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)  
  36.         {  
  37.             TextBox txtCustomerName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtCustomerName");  
  38.             TextBox txtContactName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtContactName");  
  39.             TextBox txtAddress1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtAddress1");  
  40.             TextBox txtCity = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtCity");  
  41.             TextBox txtPostalCode = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtPostalCode");  
  42.             TextBox txtCountry = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtCountry");  
  43.             TextBox txtsalary = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtsalary");  
  44.   
  45.             cmd.Connection = con;  
  46.             cmd.CommandText = "UPDATE customer SET CustomerName ='" + txtCustomerName.Text + "',ContactName ='" + txtContactName.Text + "',Address1 ='" + txtAddress1.Text + "',City ='" + txtCity.Text + "',PostalCode ='" + txtPostalCode.Text + "',Country ='" + txtCountry.Text + "',salary ='" + txtsalary.Text + "' WHERE CustomerID='" + GridView1.DataKeys[e.RowIndex].Values[0].ToString() + "'";  
  47.             con.Open();  
  48.             cmd.ExecuteNonQuery();  
  49.             GridView1.EditIndex = -1;  
  50.             display();  
  51.             con.Close();  
  52.   
  53.         }  
  54.       
  55.   
  56.     }  

Here, you can easily see that bind function has been used to populate TextBoxes instead of Eval.
 
 
I hope this blog will helps you to understand the difference between Eval and Bind function.