Datagrid上的复选框用于ASP

时间:2012-10-22 19:12:44

标签: asp.net vb.net datagridview checkbox

我的项目中有一个数据网格,其中Checkbox为TemplateField;但我无法访问checkbox.checked属性。有没有人有任何想法?

我的ASP代码:

<asp:GridView ID="GVP" runat="server" AutoGenerateColumns="False" DataSourceID="DSP">
     <Columns>
          <asp:TemplateField HeaderStyle-Width="5%" ItemStyle-Width="5%" FooterStyle-Width ="5%">
               <ItemTemplate>
                    <asp:CheckBox ID="SelectCb" runat="server"></asp:CheckBox>
               </ItemTemplate>
               <FooterStyle Width="5%"/>
               <HeaderStyle Width="5%"/>
               <ItemStyle Width="5%"/>
          </asp:TemplateField>
          <asp:BoundField DataField="Answers" HeaderText="Options" SortExpression="Answers" />
     </Columns>
</asp:GridView>

我的VB代码背后:

     Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles buttonNext.Click
    Dim SelectedBox As Boolean = False
    For Each row As GridViewRow In GVP.Rows
        Dim cb As CheckBox = row.FindControl("SelectCb")
        If cb IsNot Nothing AndAlso cb.Checked Then
            SelectedBox = True
            Dim RID As Integer = Convert.ToInt32(GVP.DataKeys(row.RowIndex).Value)
        Else
            ShowMessage("You did not select anything")
        End if

3 个答案:

答案 0 :(得分:0)

试试这个:

For Each row As GridViewRow In gvTest.Rows
    Dim cb As CheckBox = row.FindControl("SelectCb")
    If (CType(row.FindControl("SelectCb"), CheckBox)).Checked = True Then
       SelectedBox = True
       Dim RID As Integer = Convert.ToInt32(gvTest.DataKeys(row.RowIndex).Value)
    End If    
Next

答案 1 :(得分:0)

很难说你在这里尝试做什么以及你是如何测试的,但我的猜测是因为你没有检查行类型。所以第一行实际上是标题,因此根本没有复选框(你会收到消息)。

For Each row As GridViewRow In GVP.Rows
    If row.RowType = DataControlRowType.DataRow Then
        Dim cb As CheckBox ... 

答案 2 :(得分:0)

问题确实存在于Page_Load,我将网格绑定到数据源;我删除了它,问题解决了。