嵌套的GridView获取父行 [英] nested gridview get parent row

查看:207
本文介绍了嵌套的GridView获取父行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用嵌套GridView的,其中在的GridView 的有子GridView的每一行。 我使用的的RowDataBound 赛事的父GridView控件的,有约束力的子的GridView 的。 我的问题是,如何让家长GridView的儿童GridView的关键的RowDataBound 事件。

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.

下面的例子code:

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

下面是code背后:

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

希望以上code解释了所有的场景。

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>

$ C $后面

Code behind

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆