Eval()理解するための絶対に最小限のASP.NETコードを誰かに見せてもらえますBind()か?
2 つの個別のコード スニペットまたは Web リンクを提供していただけると幸いです。
読み取り専用コントロールの場合、それらは同じです。双方向のデータバインディングの場合、更新、挿入などを行うデータソースを宣言的なデータバインディングで使用するには、 を使用する必要がありますBind。
たとえば、 と を持つ GridView を想像してみてItemTemplateくださいEditItemTemplate。BindまたはEvalで使用する場合ItemTemplate、違いはありません。で使用するEvalと、グリッドがバインドされているのメソッドにEditItemTemplate値を渡すことができなくなります。UpdateDataSource
更新:私はこの例を思いつきました:
<%@ 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.5Eval()以降では、これらのバインディングを replace*およびに設定するより良い方法があり、強く型付けされた bindingsBind()を利用しています。
*注:これは、またはを使用していない場合にのみ機能します。厳密に型指定されたオブジェクト ( EF モデルまたはその他のクラスから) が必要です。SqlDataSourceanonymous object
このコード スニペットは、コントロールにどのように使用されるかEvalを示しています (上記の Darin Dimitrov によって説明されているように、必要があります。BindListViewInsertItemBindItemTemplateEval
<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、データ ソースに割り当てるオブジェクトのタイプを指す新しいプロパティで拡張されました。
<asp:ListView ItemType="Picture" ID="ListView1" runat="server" ...>
Picture厳密に型指定されたオブジェクトです (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 %>
Eval & Bind よりも優れている点:

出典:この素晴らしい本から