Gridview嵌套在另一个gridview上,获取子项和父项的数据键 [英] Gridview nested on another gridview getting the data key of the child and the parent

查看:81
本文介绍了Gridview嵌套在另一个gridview上,获取子项和父项的数据键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生/女士在这里遇到了一个大问题,因为您可以看到ive创建了一个嵌套的gridview.我的问题是在子gridview的列中有一个imagebutton;如果单击它,我想捕获父级和子级gridview的数据键.先生,可以吗?

Sir/madam got a huge problem here as you can see ive created a nested gridview. My problem is on the child gridview where in its column an imagebutton; If I click it I want to catch the data keys of the parent and child gridview. Is possible sir?

                        <asp:GridView ID="grdCategory" runat="server" AutoGenerateColumns="False"   
                            Width="900px" DataKeyNames="CourseCatID" Font-Names="verdana,arial,helvetica,sans-serif" Font-Size="8pt" CellPadding="4" ForeColor="#333333" GridLines="None">
                            <columns>
                             <asp:BoundField  HeaderText = "CourseCatID" DataField="CourseCatID" />
                             
                             <asp:TemplateField HeaderText="Course Category">
                                 <itemtemplate>
                                 
                                 
                                <a href="java<!-- no -->script:toggleDiv(''mydiv<%# Eval(" coursecatid=")%>'')"><asp:Image ID="img" onclick="javascript:Toggle(this);" runat="server" ImageUrl="~/Images/plus.gif"
                                ToolTip="Expand" Width="7px" Height="7px" ImageAlign="AbsMiddle"/></a>
                                
                                       <asp:Label ID="lbllastname" Height="15px" runat="server" Text=''<%# Eval("CourseCatName")%>''> 
           
                               
                                                                        

                                      <div id="mydiv<%# Eval(" coursecatid=")%>" style="display:none">
                                                        
                                       <br />
                               
                                     
                             
                                            

                     <asp:ImageButton ID="ImageAdd" Height="17px" ImageUrl="Images/addCourse.png" runat="server" CommandName="cmdAdd" CommandArgument=''<%# Eval("CourseCatID") %>'' OnClick="ImageAdd_click"
                                        />
                                       <br />
                                       <br />
                               <asp:GridView ID="grdCourse" runat="server"  Width="800px" HorizontalAlign="Right" AutoGenerateColumns="False" align="center" DataKeyNames="CourseName" Font-Names="verdana,arial,helvetica,sans-serif" Font-Size="8pt" BackColor="White" 
                              GridLines="Vertical">
<columns> <asp:TemplateField HeaderText="Delete" HeaderStyle-HorizontalAlign="Left"  >
                                 <itemtemplate>
                                     <asp:ImageButton ID="ImageDelete"  Height="15px" ImageUrl="Images/Delete.png" runat="server" CommandName="cmdDelete" CommandArgument=''<%# Eval("CourseName") %>''
                                        />
                                 </itemtemplate>
                                   <itemstyle horizontalalign="Left" />
                                </columns>
                               
</div></itemtemplate></columns>



这是我在RowCommand上的代码



Here is my code on RowCommand

Protected Sub grdCategory_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdCategory.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim gv As GridView = DirectCast(e.Row.FindControl("grdCourse"), GridView)
            _courselistDAL = New CourseListDAL
            Dim tpTemplate As New TrainingPlanTemplate

            tpTemplate.Year = 2011
            tpTemplate.CourseCatID = DataBinder.Eval(e.Row.DataItem, "CourseCatID")

            gv.DataSource = _courselistDAL.GetCourseDefinition(tpTemplate)
            gv.DataBind()
            grdCategory.Columns(0).Visible = False
            grdCategory.Columns(3).Visible = False
            ''gv.Columns(0).Visible = False
            gv.Columns(5).Visible = False

        End If

推荐答案

为什么不在子网格上方放置另一个标签并与数据键(primary key)绑定,并在行命令上调用
why not you put an other label just above you child grid and bind with you datakeys(primery key) and on row command you can call
int ParentKey =  Convert.ToInt32(((Label)gvChildGrid.Parent.FindControl("lblPrimeryKey")).text)



这将给出父网格的垂直行的数据键.
对于子网格,您可以轻松地在行命令中找到Commandargument.



this will give the datakeys of that perticular row of parent grid.
and for childgrid you can eaisly find on row command as commandargument.


在行命令事件中,您可以像这样从子网格访问父网格..

On row command event, you can access parent grid from child grid like this..

Protected void grdCategory_RowCommand(Object sender, GridViewRowEventArgs e)
{
   if(e.CommandName=="cmdDelete")
   {
       GridView ChildGridView = GridView child = (GridView)((ImageButton)(e.CommandSource)).Parent.Parent.Parent.Parent;
       GridView ParentGridView = (GridView)(ChildGridView.Parent.Parent.Parent.Parent);
   }
}


这篇关于Gridview嵌套在另一个gridview上,获取子项和父项的数据键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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