Mark
Mark

Reputation: 1434

Bind the datasource of a nested ListView to the parent's ListView datasource

I have triple-nested ListView controls on my asp.net page, each nested within another. I use the OnItemDataBound event in the 1st ListView to set the DataSource of the 2nd level ListView. The 3rd ListView is contained in the of the 2nd ListView. I want to assign the same DataSource to both the 2nd and 3rd level ListView datasource controls, but I cannot figure out how to access the 3rd level ListView in order to do that.

Here is some sample code to help visualize:

<asp:ListView id="level1" runat="server" OnItemDataBound="level1_ItemDataBound">
  <layouttemplate>
    <asp:PlaceHolder runat="server" ID="itemPlaceHolder"></asp:PlaceHolder>
  </layouttemplate>
  <itemtemplate>
    <asp:ListView id="level2" runat="server">
      <layouttemplate>
        <asp:ListView id="level3" runat="server">
          <layouttemplate>
            <asp:PlaceHolder runat="server" ID="itemPlaceHolder"></asp:PlaceHolder>
          </layouttemplate>
          <itemtemplate>OUTPUT DATA FOR LEVEL 3</itemtemplate>
        </asp:ListView>
      </layouttemplate>
      <itemtemplate>OUTPUT DATA FOR LEVEL 2</itemtemplate>
    </asp:ListView>
    OUTPUT DATA FOR LEVEL 1
  </itemtemplate>
</asp:ListView>

The level1_ItemDataBound method finds the level2 control, casts it as a ListView, sets its DataSource and executes the DataBind. At this point I'm stuck trying to get Level3.DataSource to be set to the same as Level2.DataSource. Any help?

Upvotes: 3

Views: 6124

Answers (1)

Dave Brace
Dave Brace

Reputation: 1809

Before you call DataBind on the on the level2 listview, you should register an event handler on the level2's ItemDataBound event.

Some psuedo code:

protected void level1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
  var listView2 = (ListView) e.Item.FindControl("level2");
  listView2.ItemDataBound += level2_ItemDataBound;
  listView2.DataSource = myDataSource;
  listView2.DataBind();
}

protected void level2_ItemDataBound(object sender, ListViewItemEventArgs e)
{
  var listView3 = (ListView) e.Item.FindControl("level3");
  listView3.DataSource = myDataSource;
  listView3.DataBind();
}

Upvotes: 2

Related Questions