【问题标题】:Understanding ASP.NET Eval() and Bind()了解 ASP.NET Eval() 和 Bind()
【发布时间】:2009-11-22 08:57:12
【问题描述】:

谁能告诉我一些绝对最小的 ASP.NET 代码来理解 Eval()Bind()

最好给我提供两个单独的code-sn-ps或者可能是网络链接。

【问题讨论】:

    标签: asp.net eval bind


    【解决方案1】:

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

    想象一个带有ItemTemplateEditItemTemplate 的GridView。如果在ItemTemplate 中使用BindEval,则没有区别。如果在EditItemTemplate中使用Eval,则该值将无法传递给网格绑定的DataSourceUpdate方法。


    更新:我想出了这个例子:

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

    【讨论】:

    • GridView 是一个复杂的控件。您能用文本框或页面本身等更简单的控件为我解释一下吗?
    • 更简单的控件没有DataSource 属性。
    • @DarinDimitrov 绑定或评估是否仅适用于 IEnumerables?如果我的源是 DataTable,我可以使用它吗?
    【解决方案2】:

    Darin Dimitrov 完美回答了这个问题,但自从 ASP.NET 4.5 以来,现在有了更好的方法来设置这些绑定来替换* Eval()Bind(),利用强类型绑定

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

    这段代码 sn-p 显示了 EvalBind 将如何用于 ListView 控件(InsertItem 需要 Bind,正如上面 Darin Dimitrov 所解释的那样,ItemTemplate 是只读的(因此它们是标签),所以只需要一个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>
    

    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 的优势

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

    来源:来自this好书

    【讨论】:

    • 那么&lt;%# 语法与&lt;%= 相比如何?
    • This 解释了&lt;%#&lt;%= 的区别。
    • 我到底是怎么知道这个功能的?!?辉煌
    猜你喜欢
    • 2010-12-20
    • 2010-09-20
    • 1970-01-01
    • 2011-08-01
    • 2014-10-25
    • 2012-08-11
    • 1970-01-01
    • 2010-09-11
    • 2011-04-27
    相关资源
    最近更新 更多