如何根据外部网格视图选择填充内部网格视图(ASP.NET/C#)

发布于 2024-08-27 13:01:10 字数 977 浏览 6 评论 0原文

我有 2 个 GridView,InnerGridView 嵌套在 OuterGridView 的 TemplateField 内。 每个 GridView 都有一个 ObjectDataSource (ODS)。我希望 InnerGridView 显示唯一的数据 添加到 OuterGridView 中列出的 GroupName。我已经用谷歌搜索这个数周了 查看基于 RowDataBound 和 ODS Selecting 事件的各种想法。

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

因此,我需要向 InnerGridView 的 ODS 发送一个参数:

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 事件内部找到任何控件。 但我不知道如何使用“e”或“sender”参数访问该层次结构。 如果我能找到各种对象和控件的解释......以及它们的有效范围 以及如何访问它们......这会很有帮助。

非常感谢,保罗

I have 2 GridViews, the InnerGridView is nested inside a TemplateField of my OuterGridView.
Each GridView has an ObjectDataSource (ODS). I want the InnerGridView to display data that is unique
to the GroupName that is listed in the OuterGridView. I have been Googling this for weeks,
seeing various ideas based around RowDataBound and the ODS Selecting event.

I don't think RowDataBound is the answer, because the Selecting event for the InnerGridView ODS
has already been called when RowDataBound is called for the OuterGridView.

So, I need to send a parameter to the ODS for the InnerGridView:

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

Note: This method is assigned to the Selecting event of the InnerGridView ODS.

My question is: How do I access the GroupName from the OuterGridView... while I am processing the InnerGridView.
Here is an example of something that does not work: OuterGridView.SelectedRow.FindControl( "GroupName" ).ToString();

I have heard that one can find any control from inside the Selecting event by searching a certain hierarchy.
But I don't know how to access that hierarchy using the "e" or "sender" parms.
If I could find an explanation of the various objects and controls... and where they have a valid scope
and how they are accessed... that would be helpful.

Many thanks, Paul

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北斗星光 2024-09-03 13:01:10

如果将发件人对象转换为 gridview 对象,则使用 SelectedRow 像这里一样并获取组名?

if you cast the sender object to a gridview object en then use the SelectedRow like here and the get the groupname?

我的鱼塘能养鲲 2024-09-03 13:01:10

如果要根据外部 GridView 的 GroupName 绑定内部 GridView,请执行以下步骤

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

If you want to bind the Inner GridView with respect to GroupName of Outer GridView do the following steps

1) if(!IsPostBack) Bind the Outer GridView.
2) In the Outer GridViews RowDataBound Event Find The Inner GridView by checking the condition
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView drv = ((DataRowView)e.Row.DataItem);
GridView InnerGridView= (GridView)e.Row.FindControl("InnerGridView");
if (InnerGridView!=null)
{
string GroupName = drv["GroupName"]; // to get the GropName.
InnerGridView.DataSource = "Query WRT the GroupName";
InnerGridView.DataBind();
}
}

生生漫 2024-09-03 13:01:10

希望以下内容对

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

}

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

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

本文改编自本文概述使用 SQL 数据源嵌套 gridview,但页面生命周期应该相同。

Hopefully The Following Helps

ASPX page:

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

Code Behind:

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

}

Note the use of a data key to hold the product group details.

Obviously you'll need to configure your object datasources and wire them up to the gridviews as needed.

This is adapted from this article outlining nesting gridviews using a SQL Datasource but the page life cycle should be the same.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文