2013-06-28 35 views
0

我嵌套了一个Gridview2(子),我想获取点击按钮上的选中行。获取按钮点击嵌套的Gridview的检查行

当我尝试从按钮点击事件访问Gridview2时,我无法做到这一点。但是,我可以访问父级Gridview1。

有人可以解释我如何获得按钮点击的Child Gridview的检查行。

此外,按钮是子Gridview的列标题。

这是我的代码。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" 
    CellPadding="3" GridLines="Horizontal" 
    onrowdatabound="GridView1_RowDataBound" DataKeyNames="id1"> 
    <AlternatingRowStyle BackColor="#F7F7F7" /> 
    <Columns> 

     <asp:TemplateField> 
     <ItemTemplate> 
        <a href="javascript:collapseExpand('id1_<%# Eval("id1") %>');"> 
         <img id="imageSubId_<%# Eval("id1") %>" alt="Click to show/hide orders" border="0" 
          src="Images/bullet_toggle_plus.jpg" /></a> 
       </ItemTemplate> 
      </asp:TemplateField> 
     <asp:BoundField DataField="id1" HeaderText="ID" /> 
     <asp:TemplateField> 
      <ItemTemplate> 
      <tr> 
      <td colspan="100%"> 
       <div id="rid1_<%# Eval("id1") %>" style="display: none; position: relative; left: 25px;"> 
       <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" CellPadding="4" ForeColor="#333333" 
        GridLines="None" OnRowCommand="Gridview2_RowCommand"> 
        <Columns> 

        <asp:BoundField DataField="fname" HeaderText="First Name" /> 
        <asp:BoundField DataField="mname" HeaderText="Middle Name" /> 
        <asp:BoundField DataField="lname" HeaderText="Last Name" /> 
        <asp:TemplateField> 
        <ItemTemplate> 

        <asp:CheckBox ID="checkselect" runat="server" /> 

        </ItemTemplate> 
        <HeaderTemplate> 
         <asp:Button ID="Button4" runat="server" Text="Remove" CommandName="Split" OnClick="Button4_Click" /> 
         <ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="Button4" PopupControlID="Panel1" CancelControlID="Button1"> 
         </ajaxToolkit:ModalPopupExtender> 

         </HeaderTemplate> 

        </asp:TemplateField> </Columns></asp:GridView> 
       </div> 
       </td> 
       </tr> 
      </ItemTemplate> 
     </asp:TemplateField> 
     </Columns> 
     </asp:GridView> 

回答

1

我会做的就是添加一个CommandArgumentButton4的宣言,让我找到了相关GridView2。

所以,我会使用你在别处使用的,id1。将CommandArgument='<%# Eval("id1") %>'添加到您的Button4声明中。

现在,在Button4_Click,你可以投发送到Button像这样:

var button4 = (Button)sender; 

一旦你正确BUTTON4浇铸为Button,您可以访问CommandArgument属性。

var id1 = button4.CommandArgument; 

一旦你有id1,就像遍历父节点GridView GridView1一样简单。看起来像你的第二个列绑定到id1一样,所以你会做到以下几点:

GridView foundGridView2; 
foreach(GridViewRow gvr in GridView1.Rows) 
{ 
    if(gvr.RowType == DataControlRowType.DataRow && gvr.Cells[1].Text == id1) 
    { 
     foundGridView2 = gvr.FindControl("GridView2"); 
     break; // Once we've found it, no need to iterate through other items 
    } 
} 

在这一点上,你现在可以访问您发现GridView2,你可以通过GridView2的行和迭代选中复选框。让我知道你是否需要帮助。

添加到答案,因为我觉得父母GridView1可吞咽将Button4的点击:

创建GridView1的OnRowCommand事件的事件处理程序。

在所得GridView1_RowCommand()方法,使用下面的处理只是你将Button4 Click事件:

if(e.CommandName == "Split") 
{ 
    // At this point I would refactor out the code you put in Button4_Click 
    // into a separate method. I'll give you an example: 
    HandleButton4Click(e.CommandArgument); // e.CommandArgument will contain the id1 
} 
+0

我应该使用 'ID1 _ <%#的eval( “ID1”)%>' 或“<%#评估和演示(“id1”)%>' – Huzaifa

+0

你试图在'上匹配它,所以如果你在前面加上“id1_”,它永远不会匹配。 –

+0

非常感谢你的驻军。我很欣赏这一点。只是最后一件事由于某种原因,按钮事件不会触发。是否因为我也在我的ajaxmodalpopup中使用它。 – Huzaifa

相关问题