【问题标题】:How to fill an inner gridview based on outer gridview selection (ASP.NET/C#)如何根据外部网格视图选择填充内部网格视图(ASP.NET/C#)
【发布时间】:2010-04-05 16:32:27
【问题描述】:

我有 2 个 GridView,InnerGridView 嵌套在我的 OuterGridView 的 TemplateField 中。 每个 GridView 都有一个 ObjectDataSource (ODS)。我希望 InnerGridView 显示唯一的数据 到 OuterGridView 中列出的 GroupName。我已经在谷歌上搜索了几个星期, 看到基于 RowDataBound 和 ODS 选择事件的各种想法。

我不认为 RowDataBound 是答案,因为 InnerGridView ODS 的 Selecting 事件 为 OuterGridView 调用 RowDataBound 时已调用。

所以,我需要向 ODS 发送 InnerGridView 的参数:

protected void ProductDataSource_Selecting( object sender, ObjectDataSourceSelectingEventArgs e ) {
        e.InputParameters["productGroup"] = <here I need to access the GroupName from the OuterGridView>;
}

注意:此方法分配给 InnerGridView ODS 的 Selecting 事件。

我的问题是:如何在处理 InnerGridView 时从 OuterGridView 访问 GroupName...。 这是一个不起作用的例子:OuterGridView.SelectedRow.FindControl("GroupName").ToString();

我听说可以通过搜索某个层次结构从 Selecting 事件内部找到 any 控件。 但我不知道如何使用“e”或“sender”参数访问该层次结构。 如果我能找到各种对象和控件的解释......以及它们的有效范围 以及如何访问它们...这将很有帮助。

非常感谢,保罗

【问题讨论】:

    标签: c# asp.net gridview nested objectdatasource


    【解决方案1】:

    如果您将 sender 对象转换为 gridview 对象然后使用SelectedRow 像这里和获取组名?

    【讨论】:

      【解决方案2】:

      如果您想将 Inner GridView 与 Outer GridView 的 GroupName 绑定,请执行以下步骤

      1) if(!IsPostBack) 绑定Outer GridView。 2) 在 Outer GridViews RowDataBound 事件中通过检查条件找到 Inner GridView if (e.Row.RowType == DataControlRowType.DataRow) { DataRowView drv = ((DataRowView)e.Row.DataItem); GridView InnerGridView= (GridView)e.Row.FindControl("InnerGridView"); if (InnerGridView!=null) { 字符串组名 = drv["组名"]; // 获取 GropName。 InnerGridView.DataSource = "查询 WRT GroupName"; InnerGridView.DataBind(); } }

      【讨论】:

        【解决方案3】:

        希望以下帮助

        ASPX 页面:

            <asp:ObjectDataSource ID="ODS1" runat="server"></asp:ObjectDataSource>
            <asp:GridView ID="GV1" runat="server" DataKeyNames="productGroup" 
                onrowdatabound="GV1_RowDataBound">
                <Columns>
                    <asp:TemplateField>
        
                        <ItemTemplate>
                            <asp:ObjectDataSource ID="ODS2" runat="server">
                            </asp:ObjectDataSource>
                            <asp:GridView ID="GV2" runat="server">
                            </asp:GridView>
                        </ItemTemplate>
        
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        

        代码背后:

        protected void GV1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
              if(e.Row.RowType == DataControlRowType.DataRow)
              {
                 string productgroup = ((GridView)sender).DataKeys[e.Row.RowIndex]["productGroup"].ToString();
                 ObjectDataSource objDS = (ObjectDataSource)e.Row.FindControl("ODS2");
                 objDS.SelectParameters["productGroup"].DefaultValue = productgroup;
              }
        
        }
        

        注意使用数据键来保存产品组详细信息。

        显然,您需要配置对象数据源并根据需要将它们连接到网格视图。

        这是改编自this article 使用 SQL 数据源概述嵌套网格视图,但页面生命周期应该相同。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-09-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多