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 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" />
<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 in my code behind
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) simply looks 开发者_如何学JAVAlike this. MedicalDays is a 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 get to the DataBind() call the error shows up. I've poked around on the tubes but nothing concrete is jumping out at me. I've tried changing the syntax to something like this
DataBinder.Eval(Container.DataItem,"id")
and the error goes away but no data makes it into my ListView either.
as I understand it DataBinder.Eval uses reflection to determine my datatype, but I am not sure how to troubleshoot this issue. Any help you could provide would be great.
Thanks Jim
Not sure if the problem is in your listview. I tried a simple repro using the following databinding code - which worked fine with the ASPX you included 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 a different control (ie. not inside of the listview?)
If you need to use declarative databinding on a control or property which doesn't support it, you can also look into building your own custom expression builder.
精彩评论