Unable to selected checkbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Wally McKean
    New Member
    • Dec 2011
    • 3

    Unable to selected checkbox

    I am unable to update table when the users clicks on the register button. My code is listed below.

    vb:
    Code:
    Dim retstruct As RegisterCourseInfo
            Dim uN As String
    
    
            For Each row As GridViewRow In GridView1.Rows
                Dim cb As CheckBox = CType(GridView1.FindControl("ChkSelect"), CheckBox)
    
                If cb IsNot Nothing AndAlso cb.Checked Then
    
                    uN = GridView1.DataKeys(row.RowIndex).Value
                    retstruct = RegisterCourseIndividual(Request.QueryString("CourseID"), Request.QueryString("SessionID"), uN, False)
                    cb.Checked = False
    
                End If
            Next
    Code:
    <asp:TemplateField InsertVisible="False">
                <ItemTemplate>
                    <asp:CheckBox ID="ChkSelector" ViewStateMode="disabled" Checked="false" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
    Would anyone have any ideas of why I am not getting the correct results?

    Thank you in advance for looking.
    Last edited by Frinavale; Dec 16 '11, 02:54 PM. Reason: Added code tags
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    There are 2 reasons why you aren't retrieving the CheckBox.
    First of all, the CheckBox exists each Row in the the GridView.
    So, the following:
    Code:
     Dim cb As CheckBox = CType(GridView1.FindControl("ChkSelect"), CheckBox)
    Should be:
    Code:
     Dim cb As CheckBox = CType(row.FindControl("ChkSelect"), CheckBox)
    The second problem is that you don't have a CheckBox with the ID "ChkSelect" in your row....you have a CheckBox with the ID "ChkSelecto r".

    So, the following will fix the problem:
    Code:
     Dim cb As CheckBox = CType(row.FindControl("ChkSelector"), CheckBox)

    Here's the example that I used to debug your problem.
    (ASP.NET code)
    Code:
    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server">
            <Columns>
                <asp:TemplateField InsertVisible="False">
                    <ItemTemplate>
                        <asp:CheckBox ID="ChkSelector" ViewStateMode="disabled" Checked="false" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:Button runat="server" ID="showCheckedRows" Text="show checked rows" />
        <br />
        <asp:Label runat="server" ID="checkedRows" />
        </form>
    </body>
    </html>
    (VB.NET code)
    Code:
    Public Class WebForm1
        Inherits System.Web.UI.Page
        Private _dt As DataTable
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Session("_dt") Is Nothing Then
                RetrieveDataSource()
            Else
                _dt = DirectCast(Session("_dt"), DataTable)
            End If
        End Sub
        Private Sub WebForm1_PreRender(sender As Object, e As System.EventArgs) Handles Me.PreRender
            GridView1.DataSource = _dt
            GridView1.DataBind()
        End Sub
    
        Private Sub showCheckedRows_Click(sender As Object, e As System.EventArgs) Handles showCheckedRows.Click
            Dim selectedRowText As New StringBuilder
            Dim index As Integer = 0
            For Each row As GridViewRow In GridView1.Rows
                Dim cb As CheckBox = CType(row.FindControl("ChkSelector"), CheckBox)
                If cb IsNot Nothing AndAlso cb.Checked Then
                    selectedRowText.Append("row: ")
                    selectedRowText.Append(index.ToString)
                    selectedRowText.Append("<br />")
                End If
                index += 1
            Next
            checkedRows.Text = selectedRowText.ToString
        End Sub
    
        Private Sub RetrieveDataSource()
            _dt = New DataTable
            _dt.Columns.Add("ID", GetType(Integer))
            _dt.Columns.Add("Name")
    
            For i As Integer = 1 To 10
                Dim dr As DataRow = _dt.NewRow
                dr("ID") = i
                dr("Name") = "Name " + i.ToString
                _dt.Rows.Add(dr)
            Next
            Session("_dt") = _dt
        End Sub
    End Class

    Comment

    • Wally McKean
      New Member
      • Dec 2011
      • 3

      #3
      Thank you for your help. I pasted the incorrect code after looking at your response and I had the correct findcontrol name. I have entered what you have supplied and when I run the page it bypasses the following lines:
      Code:
       If cb IsNot Nothing AndAlso cb.Checked Then
         selectedRowText.Append("row: ")
         selectedRowText.Append(index.ToString)
         selectedRowText.Append("<br />")
       End If
      Would you happen to know what may cause the code to bypass the if statement?

      Thank you again for your advice.
      Last edited by Frinavale; Dec 16 '11, 09:30 PM. Reason: Added code tags.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        It will bypass that case if cb is nothing or it's not checked.

        I recommend adding a new WebForm1.aspx to your project.
        Copy the code that I have for the ASP part and paste it into the form. Then copy the VB.NET code into the code behind for the page.

        I've tested it and it works fine.

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          A guess, off the top of my head, would be that you are calling the DataBind method on the GridView in the Page Load event.

          If you do this, anything that the user provided will be lost because the GridView re-binds to the original data.

          You should hold off calling the DataBind method until the PreRender event since it occurs after all of the other page events are done.

          -Frinny

          Comment

          • Wally McKean
            New Member
            • Dec 2011
            • 3

            #6
            Thank you for your help. I just figured it out. Once I put the checkbox if loop into a if Page.ispostback everything went through and updated as needed. You pointed me in the right direction and I really glad that there are people out there that help us that need it. Thanks again.

            Comment

            Working...