GridView Checked行值VB asp.net

时间:2016-01-14 22:41:00

标签: asp.net vb.net gridview checkbox

我有名为GridViewTest的GridView,GridView中的第一列是名为CheckBox1的模板字段复选框。我能够循环遍历GridView包含的所有行,并在名为Label2的标签中显示该值。我遇到的问题是,我只希望检查的行显示在Label2中。任何人都可以帮助我吗?

  

受保护的子btnDisplay_Click(发件人为对象,e为EventArgs)处理btnDisplay.Click

   Dim str As String = ""
   For i As Integer = 0 To GridViewTest.Rows.Count - 1
       str = (str + GridViewTest.Rows(i).Cells(0).Text & Convert.ToString(" >> ")) + GridViewTest.Rows(i).Cells(1).Text + " >> " + >GridViewTest.Rows(i).Cells(2).Text + "<br/>"
   Next

   Label2.Text = str
     

End Sub

PS:这是VB ASP.NET

<asp:GridView ID="GridViewTest" runat="server" 
                       AutoGenerateColumns="False" BackColor="White" BorderColor="#DEDFDE" 
                       BorderStyle="None" BorderWidth="1px" CellPadding="4" 
                       CssClass="GridviewTable" DataSourceID="SqlDataSourceDetailGrid" EnableTheming="True" 
                       Font-Names="Arial" Font-Size="8pt" ForeColor="Black" GridLines="Vertical" 
                       Height="100%" PageSize="15" TabIndex="25" Width="985px" AllowPaging="True" EnableModelValidation="True">
                       <FooterStyle BackColor="#CCCC99" />
                       <HeaderStyle BackColor="#6B696B" CssClass="Freezing" Font-Bold="True" 
                           ForeColor="#FFFFCC" Wrap="False" />
                       <RowStyle BackColor="#F7F7DE" VerticalAlign="Middle" Wrap="False" />
                       <AlternatingRowStyle BackColor="White" />
                       <Columns>

                           <asp:TemplateField>
                               <EditItemTemplate>
                                   <asp:CheckBox ID="CheckBox1" runat="server" />
                               </EditItemTemplate>
                               <ItemTemplate>
                                   <asp:CheckBox ID="CheckBox1" runat="server" />
                               </ItemTemplate>
                           </asp:TemplateField>

                           <asp:BoundField DataField="TestColumn1" HeaderText="TestColumn1" />
                           <asp:BoundField DataField="TestColumn2" HeaderText="TestColumn2" />
                           <asp:BoundField DataField="TestColumn3" HeaderText="TestColumn3" />
                           <asp:BoundField DataField="TestColumn4" HeaderText="TestColumn4" />
                           <asp:BoundField DataField="TestColumn5" HeaderText="TestColumn5" />
                           <asp:BoundField DataField="TestColumn6" HeaderText="TestColumn6" />

                       </Columns>
                       <PagerSettings PageButtonCount="20" Position="TopAndBottom" />
                       <PagerStyle BackColor="#F7F7DE" Font-Size="10pt" ForeColor="Black" 
                           HorizontalAlign="Center" VerticalAlign="Middle" />
                       <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />

1 个答案:

答案 0 :(得分:1)

如果您使用的模板字段的ID为CheckBox1,请使用Page_PreRender事件中的以下代码。

For Each row As GridViewRow In GridViewTest.Rows

   Dim result As Boolean = DirectCast(row.FindControl("CheckBox1"), CheckBox).Checked
   if result = True Then
      Label2.Text = string.Format("{0},row:{1} is {3}", Label2.Text , row.RowIndex, result);
   End If

Next