谁能向我展示一些理解Eval()
和Bind()
的绝对最小的ASP.NET代码
这是最好的,如果你提供给我两个单独的代码片段,或可能是网络链接。
发布于 2009-11-22 17:13:30
对于只读控件,它们是相同的。对于双向数据绑定,您需要使用一个数据源,在该数据源中,您需要使用声明式数据绑定进行更新、插入等操作,您需要使用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
}
}
https://stackoverflow.com/questions/1778221
复制相似问题