了解ASP.NET Eval()和Bind()

了解ASP.NET Eval()和Bind(),asp.net,eval,bind,Asp.net,Eval,Bind,谁能给我看一些绝对最少的ASP.NET代码来理解Eval()和Bind() 最好是提供两个单独的代码段或web链接。对于只读控件,它们是相同的。对于双向数据绑定,使用要使用声明性数据绑定更新、插入等的数据源,需要使用Bind 例如,想象一个GridView,其中包含ItemTemplate和EditItemTemplate。如果在ItemTemplate中使用Bind或Eval,则没有区别。如果在EditItemTemplate中使用Eval,则该值将无法传递到网格绑定到的DataSource的

谁能给我看一些绝对最少的ASP.NET代码来理解
Eval()
Bind()


最好是提供两个单独的代码段或web链接。

对于只读控件,它们是相同的。对于双向数据绑定,使用要使用声明性数据绑定更新、插入等的数据源,需要使用
Bind

例如,想象一个GridView,其中包含
ItemTemplate
EditItemTemplate
。如果在
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
    }
}
公共类CustomDataSource
{
公共类模型
{
公共字符串名称{get;set;}
}
公共IEnumerable Select()
{
返回新的[]
{
新模型{Name=“some value”}
};
}
公共无效更新(字符串名称)
{
//如果对文本框使用Bind,则将调用此方法
//您将能够获得新名称并更新
//相应的数据源
}
公共无效更新()
{
//如果对文本框使用Eval,将调用此方法
//并且您将无法获得用户指定的新名称
//进入
}
}

Darin Dimitrov完美地回答了这个问题,但由于ASP.NET 4.5,现在有了一种更好的方法来设置这些绑定,以取代*
Eval()
Bind()
,利用强类型绑定

*注意:这仅在未使用
SqlDataSource
匿名对象时有效。它需要一个强类型对象(来自EF模型或任何其他类)

此代码片段显示了
Eval
Bind
如何用于
ListView
控件(
InsertItem
需要
Bind
,如Darin Dimitrov所述,
ItemTemplate
是只读的(因此它们是标签),因此只需要
Eval
):

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 %>

与评估和绑定相比的优势

  • IntelliSense可以找到您正在使用的对象的正确属性
  • 若属性被重命名/删除,则在浏览器中查看页面之前会出现错误
  • 重命名对象上的属性时,外部工具(需要VS的完整版本)将正确重命名标记中的项

来源:来自优秀书籍

GridView是一个复杂的控件。您能用更简单的控件(如textbox或页面本身)为我解释一下吗?更简单的控件没有
DataSource
属性。@DarinDimitrov只对IEnumerables进行绑定或求值吗?如果我的源代码是DataTable,我可以使用它吗?
如何解释
的区别?我刚才是怎么发现这个特性的?!?明亮的
<%# Bind("Title") %>      
<%# Bind("Description") %>         
<%#  Eval("Title") %> 
<%# Eval("Description") %>
<%# BindItem.Title %>         
<%# BindItem.Description %>
<%# Item.Title %>
<%# Item.Description %>