了解ASP.NET Eval()和Bind()
任何人都可以看到一些绝对最小的ASP.NET代码来理解Eval()和Bind()吗? 
如果您向我提供两个单独的代码片段或者可能是networking链接,则最好。
 对于只读控件,它们是相同的。 对于双向数据绑定,使用要更新的数据源,插入等与声明性数据绑定,您需要使用Bind 。 
 想象一个带有ItemTemplate和EditItemTemplate的GridView。 如果在ItemTemplate使用Bind或Eval ,则不会有任何区别。 如果您在EditItemTemplate使用Eval ,则该值将无法传递到网格绑定的DataSource的Update方法。 
更新:我已经拿出这个例子:
 <%@ 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> 
下面是一个用作对象数据源的自定义类的定义:
 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 } } 
  Darin Dimitrov完全回答了这个问题,但从ASP.NET 4.5开始 ,现在有更好的方法来设置这些绑定来replace* Eval()和Bind() ,并利用强types的绑定 。 
  *注意:这只会在你不使用SqlDataSource或anonymous object才起作用。 它需要一个强types对象(来自EF模型或任何其他类)。 
 这个代码片段展示了如何使用Eval和Bind作为ListView控件( InsertItem需要Bind ,如上面的Darin Dimitrov所解释的,而ItemTemplate是只读的(因此它们是标签),所以只需要一个Eval ): 
 <asp:ListView ID="ListView1" runat="server" DataKeyNames="Id" InsertItemPosition="LastItem" SelectMethod="ListView1_GetData" InsertMethod="ListView1_InsertItem" DeleteMethod="ListView1_DeleteItem"> <InsertItemTemplate> <li> Title: <asp:TextBox ID="Title" runat="server" Text='<%# Bind("Title") %>'/><br /> Description: <asp:TextBox ID="Description" runat="server" TextMode="MultiLine" Text='<%# Bind("Description") %>' /><br /> <asp:Button ID="InsertButton" runat="server" Text="Insert" CommandName="Insert" /> </li> </InsertItemTemplate> <ItemTemplate> <li> Title: <asp:Label ID="Title" runat="server" Text='<%# Eval("Title") %>' /><br /> Description: <asp:Label ID="Description" runat="server" Text='<%# Eval("Description") %>' /><br /> <asp:Button ID="DeleteButton" runat="server" Text="Delete" CommandName="Delete" CausesValidation="false"/> </li> </ItemTemplate> 
 从ASP.NET 4.5+开始 ,数据绑定控件已经被扩展了一个新的属性ItemType ,它指向了你分配给它的数据源的对象的types。 
 <asp:ListView ItemType="Picture" ID="ListView1" runat="server" ...> 
  Picture是强types对象(来自EF模型)。 我们然后取代: 
 Bind(property) -> BindItem.property Eval(property) -> Item.property 
所以这:
 <%# Bind("Title") %> <%# Bind("Description") %> <%# Eval("Title") %> <%# Eval("Description") %> 
会成为这样的:
 <%# BindItem.Title %> <%# BindItem.Description %> <%# Item.Title %> <%# Item.Description %> 
优于评估与绑定的优势 :
-  智能感知可以find您正在使用的对象的正确属性   
- 如果属性被重命名/删除,您将在浏览器中查看页面之前收到错误
- 外部工具(需要完整版本的VS)将正确地重命名标记中的项目,当您重命名对象的属性
来源 :从这本优秀的书