Eval () can only be used in the context of a data management error. Why it happens? What can I do about it?

I am getting the following error: using asp.net and nHibernate.

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

      

I have this list

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

      

and this is in my code for

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) just looks like this. MedicalDays is 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;
    }

      

When I go to the DataBind () call, an error appears. I poked the phone, but nothing concrete jumped out at me. I tried to change the syntax to something like this

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

      

and the error goes away, but the data doesn't end up in my ListView.

As far as I understand, DataBinder.Eval uses reflection to determine my data type, but I'm not sure how to fix this issue. Any help you could provide would be great.

Thanks Jim

+2


a source to share


1 answer


Not sure if the problem is on your list. I tried a simple replay using the following data binding code - which worked fine with the ASPX you listed above:

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();

      



Is it possible that the binding error is coming from another control (i.e. not inside the list?)

If you need to use declarative data binding to a control or property that doesn't support it, you can also look at creating your own custom expression builder.

+1


a source







All Articles