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.
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.
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.
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.
Evaluates data-binding expressions at run time. Evaluates data-binding expressions at run time and formats the result as a string.
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 } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With