Asp.net
了解 ASP.NET Eval() 和 Bind()
誰能告訴我一些絕對最小的 ASP.NET 程式碼來理解
Eval()和Bind()?最好給我提供兩個單獨的程式碼片段或者可能是網路連結。
對於只讀控制項,它們是相同的。對於 2 路數據綁定,使用要通過聲明性數據綁定在其中更新、插入等的數據源,您需要使用
Bind.例如,想像一個帶有
ItemTemplateand的 GridViewEditItemTemplate。如果在 中使用BindorEval,ItemTemplate則沒有區別。如果在 中使用Eval,則EditItemTemplate該值將無法傳遞給網格綁定到的Update方法。DataSource更新:我想出了這個例子:
<%@ 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 } }