【问题标题】:Bind the datasource of a nested ListView to the parent's ListView datasource将嵌套 ListView 的数据源绑定到父级的 ListView 数据源
【发布时间】:2010-12-13 21:25:36
【问题描述】:

我的asp.net 页面上有三层嵌套的ListView 控件,每个都嵌套在另一个中。我使用 1st ListView 中的 OnItemDataBound 事件来设置 2nd 级 ListView 的 DataSource。第 3 个 ListView 包含在第 2 个 ListView 中。我想将相同的 DataSource 分配给 2 级和 3 级 ListView 数据源控件,但我不知道如何访问 3 级 ListView 才能做到这一点。

这里有一些示例代码有助于可视化:

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

level1_ItemDataBound 方法找到level2 控件,将其转换为ListView,设置其DataSource 并执行DataBind。在这一点上,我一直试图将 Level3.DataSource 设置为与 Level2.DataSource 相同。有什么帮助吗?

【问题讨论】:

    标签: c# asp.net web-applications listview


    【解决方案1】:

    在 level2 列表视图上调用 DataBind 之前,您应该在 level2 的 ItemDataBound 事件上注册一个事件处理程序。

    一些伪代码:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-26
      • 1970-01-01
      • 2011-10-21
      • 1970-01-01
      • 2011-07-27
      • 2011-02-02
      相关资源
      最近更新 更多