views:

1294

answers:

1

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;

+1  A: 

You need to bind the inner list on every row of the outer list in the ItemDataBoundEvent for the outer list.

So this code:

var qry2 = from q in context.Roles select q.j_Role;
LV2.DataSource = qualificationQry;
LV2.DataBind();

will need to go in an even handler for the outer lists ItemDataBound event:

protected void ContactsListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
  if (e.Item.ItemType == ListViewItemType.DataItem)
  {
    // Bind the inner list on every repeat of the outer list
    var qry2 = from q in context.Roles select q.j_Role;
    LV2.DataSource = qualificationQry;
    LV2.DataBind();
  }
}

And presumably you'll want to filter the values for the inner list based on a value from the current item of the outer list

Steve Willcock
Thanks, but can you be slightly more specific?
edited to be more specific - there's a link to the msdn docs for ItemDataBound in there too if you need any more info
Steve Willcock
See my edit to the original question - I've done what you proposed and I am still hitting the same error (I can see the event being called, and the data coming back from the DB ok there, I'm still getting the system.string property error though).
Great, glad you fixed that - sorry I missed your last comment there somehow, anyway at least it's working now :)
Steve Willcock