如何从具有子gridview的gridview获取复选框值

时间:2012-08-05 11:25:30

标签: c# asp.net gridview

当我点击按钮时,有人可以告诉我如何获取已检查行的列表(嵌套网格的复选框值(IN =“嵌套的GridView”)和每个选定行的doc号值)?

<asp:GridView ID="gvMaster" runat="server" AllowPaging="True"
 AutoGenerateColumns="False" DataKeyNames="Accountkey"
 DataSourceID="SqlDataSource1" OnRowDataBound="gvMaster_RowDataBound"> 
   <Columns>  
     <asp:TemplateField> 
       <ItemTemplate>  
         <a href="javascript:cx('customerID-<%# Eval("Accountkey") %>');">
           <img id="imagecustomerID-<%# Eval("Accountkey") %>"
            alt="Click to show/hide orders" border="0" src="Border.png" />
         </a> 
         <asp:CheckBox ID="chkselect" Checked="false" runat="server" />
       </ItemTemplate> 
     </asp:TemplateField> 
     <asp:BoundField DataField="Accountkey" /> 
     <asp:BoundField DataField="fullname" /> 
     <asp:TemplateField> 
       <ItemTemplate>  
         <tr><td colspan="100%">  
           <div id="customerID-<%# Eval("Accountkey") %>" style="..."> 
             <asp:GridView ID="nestedGridView" runat="server"
              AutoGenerateColumns="False" DataKeyNames="Id"> 
               <Columns> 
                 <asp:TemplateField> 
                   <ItemTemplate>  
                     <asp:CheckBox ID="chkselect" Checked="false"
                      runat="server" />
                   </ItemTemplate> 
                 </asp:TemplateField>
                 <asp:BoundField DataField="Id" HeaderText="Id"/> 
                 <asp:BoundField DataField="Valuedate" HeaderText="Valuedate"/>
                 <asp:BoundField DataField="Docnumber" HeaderText="Docnumber"/>
               </Columns>
             </asp:GridView>
           </div>  
         </td></tr>  
       </ItemTemplate>  
     </asp:TemplateField> 
   </Columns>  
</asp:GridView>

2 个答案:

答案 0 :(得分:2)

首先获取对子GridView的引用,然后使用FindControl获取其中的CheckBox:

foreach (GridViewRow row in gvMaster.Rows) 
{
    if (row.RowType == DataControlRowType.DataRow) 
    {
        GridView gvChild = (GridView) row.FindControl("nestedGridView");
        // Then do the same method for check box column 
        if (gvChild != null)
        {
            foreach (GridViewRow row in gvChild .Rows) 
            {
                if (row.RowType == DataControlRowType.DataRow) 
                {
                    CheckBox chk = (CheckBox) row.FindControl("chkselect");
                    if (chk.Checked)
                    {
                        // do your work
                    }
                }
            }
        }
    }
}

答案 1 :(得分:1)

您可以通过DataRow GridView获取它并将控件转换为CheckBox:

foreach (GridViewRow row in gvMaster.Rows)
{
    if (row.RowType == DataControlRowType.DataRow)
    {
        CheckBox chk = row.FindControl("chkselect") as CheckBox;
        if (chk.Checked)
        {
            // do your work
        }
    }
}