首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >了解绑定Eval()和ASP.NET ()

了解绑定Eval()和ASP.NET ()
EN

Stack Overflow用户
提问于 2009-11-22 16:57:13
回答 1查看 144.8K关注 0票数 59

谁能向我展示一些理解Eval()Bind()的绝对最小的ASP.NET代码

这是最好的,如果你提供给我两个单独的代码片段,或可能是网络链接。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-11-22 17:13:30

对于只读控件,它们是相同的。对于双向数据绑定,您需要使用一个数据源,在该数据源中,您需要使用声明式数据绑定进行更新、插入等操作,您需要使用Bind

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

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

代码语言:javascript
复制
<%@ 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>

下面是作为对象数据源的自定义类的定义:

代码语言:javascript
复制
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
    }
}
票数 82
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1778221

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档