C# Eval()只能在数据绑定控件错误的上下文中使用。为什么会这样?我能怎么办?

C# Eval()只能在数据绑定控件错误的上下文中使用。为什么会这样?我能怎么办?,c#,asp.net,nhibernate,data-binding,C#,Asp.net,Nhibernate,Data Binding,我在使用asp.net和nHibernate时遇到以下错误 Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control. 我有这个列表视图 <asp:ListView ID="CurrentHourList" runat="server" ItemPlaceholderID="itemPlaceholder">

我在使用asp.net和nHibernate时遇到以下错误

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
我有这个列表视图

<asp:ListView ID="CurrentHourList" runat="server" ItemPlaceholderID="itemPlaceholder">
                <LayoutTemplate>
                    <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
                </LayoutTemplate>
                <ItemTemplate>
                    <asp:ImageButton ID="DeleteDateButton" CausesValidation="false" runat="server" CommandName="Delete"
                        ImageUrl="img/delete.png" />
                    &nbsp; <span style="display: none">
                        <asp:Label ID="Id" runat="server" Text='<%# Eval("Id") %>'></asp:Label></span>
                    <asp:Label ID="Date" Text='<%# Eval("Date", "{0:dd MMM yyyy}") %>' runat="server"></asp:Label>
                    <asp:DropDownList ID="MedicalType" runat="server" SelectedValue='<%# Eval("MedicalType.Id") %>'>
                        <asp:ListItem Text="Paid" Value="1"></asp:ListItem>
                        <asp:ListItem Text="Unpaid" Value="2"></asp:ListItem>
                        <asp:ListItem Text="Special" Value="3"></asp:ListItem>
                    </asp:DropDownList>
                    Half-Day:<asp:CheckBox ID="HalfDay" runat="server" Checked='<%# Eval("IsHalfDay") %>' />
                    <br />
                </ItemTemplate>
            </asp:ListView>
MakeMedicalDays(id)就是这样的。MedicalDays是一个IList

internal IEnumerable MakeMedicalDays(string id)
    {
        int theId = 0;
        if (int.TryParse(id, out theId))
        {
            MedicalRequest r = MedicalRequest.Get(theId);
            return r.MedicalDays;
        }
        return null;
    }
当我调用DataBind()时,就会出现错误。我在管子上戳了戳,但没有任何混凝土向我跳出来。我试着把语法改成这样

DataBinder.Eval(Container.DataItem,"id")
错误消失了,但也没有数据进入我的ListView

据我所知,DataBinder.Eval使用反射来确定我的数据类型,但我不确定如何解决这个问题。你能提供的任何帮助都会很好

谢谢
Jim不确定问题是否在您的listview中。我使用以下数据绑定代码尝试了一个简单的重新编程-这在上面包含的ASPX中运行良好:

this.CurrentHourList.DataSource = new[] { new { Id = 1, Date = DateTime.Now, MedicalType = new { Id = 1 }, IsHalfDay = false }, new { Id = 1, Date = DateTime.Now, MedicalType = new { Id = 1 }, IsHalfDay = false } };
this.CurrentHourList.DataBind();
绑定错误是否可能来自不同的控件(即不在listview内部?)

如果需要在不支持声明性数据绑定的控件或属性上使用声明性数据绑定,还可以考虑构建自己的自定义表达式生成器

this.CurrentHourList.DataSource = new[] { new { Id = 1, Date = DateTime.Now, MedicalType = new { Id = 1 }, IsHalfDay = false }, new { Id = 1, Date = DateTime.Now, MedicalType = new { Id = 1 }, IsHalfDay = false } };
this.CurrentHourList.DataBind();