简体   繁体   English

如何从带有子GridView的GridView获取复选框值

[英]How to get checkbox value from a gridview with a child gridview

Can somebody tell me how can I get the list of checked lines (checkbox value from nested grid(ID="nestedGridView") and docnumber value for each selected row) when i click on a button? 有人可以告诉我单击按钮时如何获取选中的行的列表(嵌套网格的复选框值(ID =“ nestedGridView”)和每个选定行的docnumber值)吗?

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

First get a reference to the child GridView, then use FindControl to get the CheckBox inside it: 首先获取对子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
                    }
                }
            }
        }
    }
}

You can get it through DataRow GridView and casting control as CheckBox: 您可以通过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
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM