Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding ASP.NET Eval() and Bind()

Tags:

asp.net

bind

eval

Can anyone show me some absolutely minimal ASP.NET code to understand Eval() and Bind()?

It is best if you provide me with two separate code-snippets or may be web-links.

like image 867
user366312 Avatar asked Nov 22 '09 08:11

user366312


People also ask

What is the difference between Eval and bind?

The main difference between eval and bind is eval is read only, we can't change on database thing eval . While using bind we can appply some change to the database. Show activity on this post. The main difference between eval and bind is eval is read only, we can't change on database thing eval.

What is Eval function in asp net?

The Eval method takes the name of a data field and returns a string containing the value of that field from the current record in the data source. You can supply an optional second parameter to specify a format for the returned string.

What is binding in asp net?

The term 'data binding in ASP.NET simply means that the data that is collected from a source is bound to the controls that provide the read and write connection to the data. Databases, XML files, flat files, etc are some of the data sources we are talking about here.

What does Databinder Eval do?

Evaluates data-binding expressions at run time. Evaluates data-binding expressions at run time and formats the result as a string.


1 Answers

For read-only controls they are the same. For 2 way databinding, using a datasource in which you want to update, insert, etc with declarative databinding, you'll need to use Bind.

Imagine for example a GridView with a ItemTemplate and EditItemTemplate. If you use Bind or Eval in the ItemTemplate, there will be no difference. If you use Eval in the EditItemTemplate, the value will not be able to be passed to the Update method of the DataSource that the grid is bound to.


UPDATE: I've come up with this example:

<%@ Page Language="C#" %> <!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>Data binding demo</title> </head> <body>     <form id="form1" runat="server">         <asp:GridView              ID="grdTest"              runat="server"              AutoGenerateEditButton="true"              AutoGenerateColumns="false"              DataSourceID="mySource">             <Columns>                 <asp:TemplateField>                     <ItemTemplate>                         <%# Eval("Name") %>                     </ItemTemplate>                     <EditItemTemplate>                         <asp:TextBox                              ID="edtName"                              runat="server"                              Text='<%# Bind("Name") %>'                          />                     </EditItemTemplate>                 </asp:TemplateField>             </Columns>         </asp:GridView>     </form>      <asp:ObjectDataSource          ID="mySource"          runat="server"         SelectMethod="Select"          UpdateMethod="Update"          TypeName="MyCompany.CustomDataSource" /> </body> </html> 

And here's the definition of a custom class that serves as object data source:

public class CustomDataSource {     public class Model     {         public string Name { get; set; }     }      public IEnumerable<Model> Select()     {         return new[]          {             new Model { Name = "some value" }         };     }      public void Update(string Name)     {         // This method will be called if you used Bind for the TextBox         // and you will be able to get the new name and update the         // data source accordingly     }      public void Update()     {         // This method will be called if you used Eval for the TextBox         // and you will not be able to get the new name that the user         // entered     } } 
like image 80
Darin Dimitrov Avatar answered Nov 09 '22 15:11

Darin Dimitrov