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

标签 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.

我有这个 ListView

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

这在我后面的代码中

private void BindData(string id)
    {
        MedLeaveHelper = new MedicalLeaveHelper();
        DTO.MedLeaveDTO dto = MedLeaveHelper.GetMedicalRequest(id);
        if (dto != null)
        {
            EnableForm();
            this.RequestId.Text = dto.Id.ToString();
            this.ddlEmployeeName.SelectedItem.Text = dto.User;
            this.Note.Text = dto.Note;

            this.CurrentHourList.DataSource = MedLeaveHelper.MakeMedicalDays(id);      
            this.CurrentHourList.DataBind();

        }

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() 时,错误出现了。我在 pipe 上四处寻找,但没有任何具体的东西向我跳来跳去。我试过将语法更改为类似这样的内容

DataBinder.Eval(Container.DataItem,"id")

错误消失了,但也没有数据进入我的 ListView。

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

谢谢 吉姆

最佳答案

不确定问题是否出在您的 ListView 中。我使用以下数据绑定(bind)代码尝试了一个简单的重现 - 它与上面包含的 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();

绑定(bind)错误是否可能来自不同的控件(即不在 ListView 内部?)

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

关于c# - Eval(),只能在数据绑定(bind)控件错误的上下文中使用。为什么会这样?我该怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2735467/

相关文章:

c# - 将 DWORD 从 C++ 返回到 C# (DllImport)

c# - Azure 上的 EF6 连接到远程 MySQL DB 时出现间歇性问题

c# - 在 Visual Studio 中引用项目 "readonly"?

nHibernate 共享引用?

mysql - 使用 NHibernate QueryOver<> 获取表中的所有字段

c# - 如何在 resharper 插件中提供具有 datacontext 类型的智能感知?

asp.net - ASP.net中GridView/DetailsView/FormView之间的主要区别是什么?

c# - 从sql查询中获取数据到文本框中

asp.net - MVC beta 中的 <%=Html.RadioButtonList()%> 扩展在哪里?

c# - 在没有 N+1 的情况下通过 NHibernate 进行查询 - 包含示例