Eval和Bind的区别(不是其他人说的那个,突然发现的)

本文探讨了ASP.NET中GridView控件使用Eval与Bind方法的区别,特别是在自定义类和属性的绑定过程中遇到的问题及可能的原因。提供了具体的代码示例。
       
IList<A> As = GetAs();
GridViewList.DataSource = As;
GridViewList.DataBind();

其中B是自定义的一个类是A类中的一个属性,Name是B类中的一个属性绑定GridViewList上的As是A类的一个泛型集合
下面的绑定Eval可以正确现实,但是换成Bind报错,为什么不清楚
<asp:TemplateField HeaderText="Page Title">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("B.Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Page Title">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("B.Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>


public class A
{
    private B _b;
    public B B
    {
        get
        {
            if (_b == null)
            {
                _b = new B();
            }
            return _b;
        }
        set
        {
            _b = value;
        }
    }
}
public class B
{
    private string _name;
    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            _name = value;
        }
    }
}

二.

<asp:TemplateField HeaderText="EntryDate">
                                    <ItemTemplate>
                                        <asp:Label ID="LabelEntryDate" runat="server" Text='<%#Convert.ToDateTime(Eval("EntryDate")).ToString("yyyy-MM-dd HH:mm:ss") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>

用下面就报错

<asp:TemplateField HeaderText="EntryDate">
                                    <ItemTemplate>
                                        <asp:Label ID="LabelEntryDate" runat="server" Text='<%#Convert.ToDateTime(Bind("EntryDate")).ToString("yyyy-MM-dd HH:mm:ss") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值