I have a listview, where I set the datasource in the code-behind - this works fine. When I add another listview (or databound control) to the listview's itemtemplate, and set the datasource for that control in the codebehind, the fields returned by the query seem to be unavailable to the nested listview; ASP.NET throws the following error: DataBinding: 'System.String' does not contain a property with the name 'j_Name'.
In the example below, d_Description works fine, whereas j_Role throws the error indicated above. I can see the data being returned by the query, and I know the column names match, so what's causing the error (and how do I solve it)?
ASPX page
<asp:ListView ID="LV1" runat="server">
<LayoutTemplate>
<table runat="server" id="tblSummary">
<tr runat="server" id="itemPlaceholder" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<%#Eval("d_Description")%>
</td>
</tr>
<tr>
<td>
<asp:ListView ID="LV2" runat="server">
<ItemTemplate>
<%#Eval("j_Role")%>
</ItemTemplate>
<LayoutTemplate>
<asp:placeholder id="itemPlaceholder" runat="server" />
</LayoutTemplate>
</asp:ListView>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
CODE BEHIND
var qry1 = from q in context.Descriptions select q.d_Description;
LV1.DataSource = qualificationQry;
LV1.DataBind();
var qry2 = from q in context.Roles select q.j_Role;
LV2.DataSource = qualificationQry;
LV2.DataBind();
EDIT: I've added code similar to that below to the ItemDataBound event of the outer listview, and I am still encountering the same error. Presumably I am misunderstanding the instructon?
protected void LV_ItemDataBound(object sender, ListViewItemEventArgs e)
{
using (dbDataContext context = new dbDataContext()
{
var qry2 = from q in context.Roles select q.j_Role;
ListView tempLV = (ListView)e.Item.FindControl("LV2");
tempLV.DataSource = qry2;
tempLV.DataBind();
}
}
EDIT: 2 After reading around the web some more (now that I have an idea of what to search for) the proposed answer seems to be correct - however, it's not working - can anyone suggest why?
EDIT: 3 If I ditch the outputting of j_Name, and just have a hardcoded string, there is no error, and the hard-coded string ouputs the expected number of times. This would indicate that it's just the column name (j_Name) that is wrong - even though I can see the dataset coming back from the query with that exact column name.
EDIT: 4
Fixed it.
This is wrong
var qry2 = from q in context.Roles select q.j_Role;
This is correct
var qry2 = from q in context.Roles select q;