use*_*312 54 asp.net bind eval
任何人都可以向我展示一些绝对最小的ASP.NET代码来理解Eval()
和Bind()
?
最好是为您提供两个单独的代码段或可能是Web链接.
Dar*_*rov 78
对于只读控件,它们是相同的.对于2路数据绑定,使用数据源,您要在其中使用声明性数据绑定更新,插入等,您需要使用Bind
.
想象一下,例如带有ItemTemplate
和的GridView EditItemTemplate
.如果您使用Bind
或Eval
在中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>
Run Code Online (Sandbox Code Playgroud)
以下是用作对象数据源的自定义类的定义:
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
}
}
Run Code Online (Sandbox Code Playgroud)
ben*_*bia 23
Darin Dimitrov完美地回答了这个问题,但是从ASP.NET 4.5开始,现在有一种更好的方法来设置这些绑定来替换*Eval()
并Bind()
利用强类型绑定.
*注意:这仅在您不使用SqlDataSource
或使用时才有效anonymous object
.它需要一个强类型对象(来自EF模型或任何其他类).
此代码段显示了如何Eval
以及Bind
将用于ListView
控件(InsertItem
需要Bind
,如上面的Darin Dimitrov所解释的那样,并且ItemTemplate
是只读的(因此它们是标签),所以只需要a 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>
Run Code Online (Sandbox Code Playgroud)
从ASP.NET 4.5+开始,数据绑定控件已扩展为一个新属性ItemType
,该属性指向您为其数据源分配的对象类型.
<asp:ListView ItemType="Picture" ID="ListView1" runat="server" ...>
Run Code Online (Sandbox Code Playgroud)
Picture
是强类型对象(来自EF模型).然后我们替换:
Bind(property) -> BindItem.property
Eval(property) -> Item.property
Run Code Online (Sandbox Code Playgroud)
所以这:
<%# Bind("Title") %>
<%# Bind("Description") %>
<%# Eval("Title") %>
<%# Eval("Description") %>
Run Code Online (Sandbox Code Playgroud)
会变成这样的:
<%# BindItem.Title %>
<%# BindItem.Description %>
<%# Item.Title %>
<%# Item.Description %>
Run Code Online (Sandbox Code Playgroud)
优于Eval和Bind的优势:
资料来源:来自这本优秀的书
归档时间: |
|
查看次数: |
127453 次 |
最近记录: |