0

我的项目中有一个数据网格,其中一个复选框作为模板字段;但我无法访问该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
4

3 回答 3

0

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

For Each row As GridViewRow In GVP.Rows
    If row.RowType = DataControlRowType.DataRow Then
        Dim cb As CheckBox ... 
于 2012-10-23T05:02:29.840 回答
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
于 2012-10-22T19:24:01.827 回答
0

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

于 2012-10-24T16:08:03.873 回答