0

有人可以告诉我单击按钮时如何获取选中行的列表(嵌套网格中的复选框值(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>
4

2 回答 2

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
                    }
                }
            }
        }
    }
}
于 2012-08-05T12:27:02.250 回答
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
        }
    }
}
于 2012-08-05T11:43:26.470 回答