Checking if atleast one checkbox has been checked using JavaScript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • uicouic
    New Member
    • Jan 2009
    • 24

    Checking if atleast one checkbox has been checked using JavaScript

    Hi all. I need JavaScript to validate that atleast one checkbox has been checked in a form before the record(s) can be deleted.

    - I have an <asp:button id="btnDelete". ..> and the checkbox is created via grdData_ItemDat aBound with id="chkRecordId ":

    Code:
    Protected Sub grdData_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles grdData.ItemDataBound
    
            If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <> ListItemType.Footer Then
                e.Item.Cells(1).Text = "<input type='checkbox' name='chkRecordId' value='" & e.Item.Cells(0).Text & "'/>"
            End If
    
    End Sub
    As advised in another forum, I have the following JS code:

    Code:
    <script language="javascript" event="onclick" for="btnDelete">
     function validateCheckboxes() {
        var blnAllOkay = new Boolean(true);
        var strMessage = new String("");
        var checkboxes = document.getElementsByName("chkRecordId");
    
        for (var i = 0; i < checkboxes.lengths; i++) {
        if (checkboxes[i].checked == false) {
        strMessage += "<li>Please select minimum one competition category record to delete<br>";
        blnAllOkay = false;
        }
        }
     
        if (blnAllOkay != true) {
        window.event.returnValue=false;
        lblMessage.innerHTML = strMessage;
        }
        }
    </script>
    It is still unable to work though, after calling the function via btnDelete.
    Error has something to do with the array String.

    Please advise, thanks.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Please post the error message you're getting.

    This wont crash your JavaScript but, you are attempting to concatenate list items to your string:
    Code:
    strMessage += "<li>Please select minimum one competition category record to delete<br>";
    You are not closing the list item though..

    Comment

    • uicouic
      New Member
      • Jan 2009
      • 24

      #3
      The error message: "Object reference not set to an instance of an object."
      The two lines in particular I feel are

      Code:
      strRecordIds = Request.Form("chkRecordId") 
      'Use the Request.Form to collect data from checkboxes
      
      aryRecordId = strRecordIds.Split(",") 
      'Use the Split method to generate an Array, aryRecordId
      About the list item, I have no problems so far using it for other functions, but taking into account your advice, I'll close it.

      Hope I have provided enough relevant information. Thanks.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Oh, ok. Next time you have an error please specify whether its occurring client side or server side. I didn't realize your problem was happening on the server.


        Anyways, I'm not sure what you're trying to accomplish in your server side code here. You're probably getting the exception because the following line is returning null/nothing:
        Code:
        strRecordIds = Request.Form("chkRecordId")
        And then you are attempting to use the Split() method on null/nothing.

        Instead of attempting to retrieve the Checked CheckBoxes using Request.Form you should loop through the GridView, using the FindControl("ch kRecordId") to retrieve the check box for each row, and create your array server side (since you're using it server side).


        For example:
        Code:
        Dim dr As GridViewRow
        Dim itemsToDelete As List(Of Integer)
        
        For Each dr In myGridView.Rows
           Dim chkBox As CheckBox = CType(dr.FindControl("chkRecordId"), CheckBox)
            If chkBox.Checked = True Then
                 Dim x As String = dr.Cells(1).Text 'grab the cell with the aryRecordId in it
                 Dim aryRecordId As Integer = Integer.Parse(x)
                 itemsToDelete.Add(aryRecordId)
            End If
        Next

        Comment

        • uicouic
          New Member
          • Jan 2009
          • 24

          #5
          Oh thanks alot Frinavale! Its working now.

          Cheers. :)

          Comment

          Working...