嵌套 gridview 获取父行

编程入门 行业动态 更新时间:2024-10-25 16:19:59
本文介绍了嵌套 gridview 获取父行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在使用嵌套的 GridViews,其中 gridview 中的每一行都有子 gridView.我正在使用 Parent GridViewRowDataBound 事件来绑定 Child GridView.我的问题是,如何在子 gridViews RowDataBound 事件上获取父 GridView 的键.

I am using Nested GridViews where each row in the gridview has child gridView. I am using RowDataBound Event of Parent GridView, to Binding Child GridView. My Problem is that, how to get Parent GridView's Key on Child gridViews RowDataBound Event.

以下是示例代码:

<asp:GridView ID="gvParent" DataKeyNames="ID" runat="server" PageSize="1" AllowPaging="true" PagerSettings-Mode="NextPrevious" AutoGenerateColumns="False" SkinID="GVCenter" onrowdatabound="gvParent_RowDataBound">
   <Columns>
        <asp:BoundField DataField="Name" />
        <asp:TemplateField>
           <ItemTemplate>
               <asp:GridView ID="gvChild"  DataKeyNames="ID" runat="server" AutoGenerateColumns="false" ShowHeader="false" OnRowDataBound="gvChild_RowDataBound">
                  <Columns>
                     <asp:BoundField DataField="Name" />                     
                  </Columns>
                </asp:GridView>
           </ItemTemplate>
        </asp:TemplateField>
   </Columns>
</asp:GridView>

这是背后的代码:

    protected void gvParent_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            GridView gvChild= (GridView)e.Row.FindControl("gvChild");
            gvChild.DataSource = getChildObj();
            gvChild.DataBind();
        }
    }

   protected void gvChild_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // Here I need to get the parent gridview Row Key
        }
    }

希望上面的代码解释了所有的场景.

Hope the above code explains all the scenario.

提前致谢桑迪

推荐答案

试试这个

 <asp:GridView ID="gvParent" DataKeyNames="ID" runat="server" PageSize="10" AllowPaging="true"
            PagerSettings-Mode="NextPrevious" AutoGenerateColumns="False" OnRowDataBound="gvParent_RowDataBound">
            <Columns>
                <asp:BoundField DataField="Name" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:HiddenField ID="HdnID" runat="server" Value='<%# Eval("ID") %>' />
                        <asp:GridView ID="gvChild" DataKeyNames="ID" runat="server" AutoGenerateColumns="false"
                            ShowHeader="false" OnRowDataBound="gvChild_RowDataBound">
                            <Columns>
                                <asp:BoundField DataField="Name" />
                            </Columns>
                        </asp:GridView>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

背后的代码

protected void gvParent_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            GridView gvChild = (GridView)e.Row.FindControl("gvChild");
            gvChild.DataSource = GetData();
            gvChild.DataBind();
        }
    }

    protected void gvChild_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string ID = ((HiddenField)e.Row.Parent.Parent.Parent.FindControl("HdnID")).Value;
        }
    }

这篇关于嵌套 gridview 获取父行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-01 10:06:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/828291.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   gridview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!