我正在使用嵌套网格视图,其中网格视图中的每一行都有子网格视图。我正在使用Parent GridView的RowDataBound事件来绑定Child GridView。我的问题是,如何在子 gridViews RowDataBound事件上获取父 GridView 的键。
下面是示例代码:
<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
}
}
希望上面的代码解释了所有的场景。
提前感谢桑迪