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.
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- SET ANSI_PADDING ON
- GO
- CREATE TABLE [dbo].[Customer](
- [CustomerID] [int] IDENTITY(1,1) NOT NULL,
- [CustomerName] [varchar](50) NULL,
- [ContactName] [varchar](50) NULL,
- [Address1] [varchar](100) NULL,
- [City] [varchar](50) NULL,
- [PostalCode] [varchar](50) NULL,
- [Country] [varchar](50) NULL,
- [salary] [int] NULL
- ) ON [PRIMARY]
- GO
- SET ANSI_PADDING OFF
- GO
- SET IDENTITY_INSERT [dbo].[Customer] ON
- 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)
- 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)
- 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)
- 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)
- 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)
- 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)
- SET IDENTITY_INSERT [dbo].[Customer] OFF
The [Customer] table,now look like this after data insertion,
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.
The code behind it is
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.Configuration;
- using System.Data.SqlClient;
- using System.Data;
-
- namespace GRIDOPERATION.GRIDVIEW_PROJECT
- {
- public partial class Eval_Bind : System.Web.UI.Page
- {
- string strConnString = WebConfigurationManager.ConnectionStrings["ConDB"].ConnectionString;
-
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!this.IsPostBack)
- {
- gedata();
- }
- }
- public void gedata()
- {
-
- SqlConnection con = new SqlConnection(strConnString);
- SqlCommand cmd = new SqlCommand("[display_data]", con);
- cmd.CommandType = CommandType.StoredProcedure;
- con.Open();
- SqlDataAdapter sda = new SqlDataAdapter(cmd);
- DataSet ds = new DataSet();
- sda.Fill(ds);
- GridView1.DataSource = ds;
- GridView1.DataBind();
- con.Close();
-
- }
- }
- }
Here I created a procedure named "[display_data]"
- /****** Object: StoredProcedure [dbo].[display_data] Script Date: 07/25/2019 10:14:21 ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- ALTER procedure [dbo].[display_data]
-
- as
- begin
- SELECT [CustomerID]
- ,[CustomerName]
- ,[ContactName]
- ,[Address1]
- ,[City]
- ,[PostalCode]
- ,[Country]
- ,[salary]
- FROM [Customer]
- end
The following screenshot displays the GridView consisting of TextBoxes whose values are populated using
Eval function.
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.
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Bind.aspx.cs" Inherits="GRIDOPERATION.GRIDVIEW_PROJECT.Bind" %>
-
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowUpdating="GridView1_RowUpdating" DataKeyNames="CustomerID" >
- <Columns>
- <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" InsertVisible="False"
- ReadOnly="True" SortExpression="CustomerID" />
- <asp:TemplateField HeaderText="CustomerName" ItemStyle-Width="100">
- <ItemTemplate>
- <asp:TextBox ID="txtCustomerName" runat="server" Text='<%# Bind("CustomerName") %>' />
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="ContactName" ItemStyle-Width="80">
- <ItemTemplate>
- <asp:TextBox ID="txtContactName" runat="server" Text='<%# Bind("ContactName") %>' />
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="Address1" ItemStyle-Width="100">
- <ItemTemplate>
- <asp:TextBox ID="txtAddress1" runat="server" Text='<%# Bind("Address1") %>' />
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="City" ItemStyle-Width="80">
- <ItemTemplate>
- <asp:TextBox ID="txtCity" runat="server" Text='<%# Bind("City") %>' />
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="PostalCode" ItemStyle-Width="100">
- <ItemTemplate>
- <asp:TextBox ID="txtPostalCode" runat="server" Text='<%# Bind("PostalCode") %>' />
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="Country" ItemStyle-Width="80">
- <ItemTemplate>
- <asp:TextBox ID="txtCountry" runat="server" Text='<%# Bind("Country") %>' />
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="salary" ItemStyle-Width="100">
- <ItemTemplate>
- <asp:TextBox ID="txtsalary" runat="server" Text='<%# Bind("salary") %>' />
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="" ItemStyle-Width="100">
- <ItemTemplate>
- <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
- </ItemTemplate>
- </asp:TemplateField>
- </Columns>
- </asp:GridView>
- </div>
- </form>
- </body>
- </html>
The code behind it is
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data;
- using System.Data.SqlClient;
- using System.Configuration;
-
- namespace GRIDOPERATION.GRIDVIEW_PROJECT
- {
- public partial class Bind : System.Web.UI.Page
- {
- SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConDB"].ToString());
- SqlDataAdapter sda = new SqlDataAdapter();
- SqlCommand cmd = new SqlCommand();
- DataTable dataTable;
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- display();
- }
-
- private void display()
- {
- dataTable = new DataTable();
- cmd.Connection = con;
- cmd.CommandText = "SELECT * FROM Customer";
- sda = new SqlDataAdapter(cmd);
- sda.Fill(dataTable);
- GridView1.DataSource = dataTable;
- GridView1.DataBind();
- }
- protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
- {
- TextBox txtCustomerName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtCustomerName");
- TextBox txtContactName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtContactName");
- TextBox txtAddress1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtAddress1");
- TextBox txtCity = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtCity");
- TextBox txtPostalCode = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtPostalCode");
- TextBox txtCountry = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtCountry");
- TextBox txtsalary = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtsalary");
-
- cmd.Connection = con;
- 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() + "'";
- con.Open();
- cmd.ExecuteNonQuery();
- GridView1.EditIndex = -1;
- display();
- con.Close();
-
- }
-
-
- }
- }
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.