473,461 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Checking if atleast one checkbox has been checked using JavaScript

24
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_ItemDataBound with id="chkRecordId":

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

Expand|Select|Wrap|Line Numbers
  1. <script language="javascript" event="onclick" for="btnDelete">
  2.  function validateCheckboxes() {
  3.     var blnAllOkay = new Boolean(true);
  4.     var strMessage = new String("");
  5.     var checkboxes = document.getElementsByName("chkRecordId");
  6.  
  7.     for (var i = 0; i < checkboxes.lengths; i++) {
  8.     if (checkboxes[i].checked == false) {
  9.     strMessage += "<li>Please select minimum one competition category record to delete<br>";
  10.     blnAllOkay = false;
  11.     }
  12.     }
  13.  
  14.     if (blnAllOkay != true) {
  15.     window.event.returnValue=false;
  16.     lblMessage.innerHTML = strMessage;
  17.     }
  18.     }
  19. </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.
Jan 22 '09 #1
4 6592
Frinavale
9,735 Expert Mod 8TB
Please post the error message you're getting.

This wont crash your JavaScript but, you are attempting to concatenate list items to your string:
Expand|Select|Wrap|Line Numbers
  1. strMessage += "<li>Please select minimum one competition category record to delete<br>";
You are not closing the list item though..
Jan 22 '09 #2
uicouic
24
The error message: "Object reference not set to an instance of an object."
The two lines in particular I feel are

Expand|Select|Wrap|Line Numbers
  1. strRecordIds = Request.Form("chkRecordId") 
  2. 'Use the Request.Form to collect data from checkboxes
  3.  
  4. aryRecordId = strRecordIds.Split(",") 
  5. '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.
Jan 23 '09 #3
Frinavale
9,735 Expert Mod 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. 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("chkRecordId") to retrieve the check box for each row, and create your array server side (since you're using it server side).


For example:
Expand|Select|Wrap|Line Numbers
  1. Dim dr As GridViewRow
  2. Dim itemsToDelete As List(Of Integer)
  3.  
  4. For Each dr In myGridView.Rows
  5.    Dim chkBox As CheckBox = CType(dr.FindControl("chkRecordId"), CheckBox)
  6.     If chkBox.Checked = True Then
  7.          Dim x As String = dr.Cells(1).Text 'grab the cell with the aryRecordId in it
  8.          Dim aryRecordId As Integer = Integer.Parse(x)
  9.          itemsToDelete.Add(aryRecordId)
  10.     End If
  11. Next
  12.  
Jan 23 '09 #4
uicouic
24
Oh thanks alot Frinavale! Its working now.

Cheers. :)
Jan 28 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Fred | last post by:
Hi, I defined a form consisting of checkboxes like: <form> <input type="checkbox" name=ck id=ck onclick="check(this.form)" <input type="checkbox" name=ck id=ck onclick="check(this.form)" ........
2
by: Menelty | last post by:
I have 2 datagrids on my page and each one has a template column which has a checkbox relating to each specific row. I usually have one on the page and use javascript to make sure there has been x...
12
by: Randell D. | last post by:
Folks, I have a form called "ourTestForm". Its a test form - nothing special - it contains five input tags - they are named one, two, three, four and five. The input tags are of type...
1
by: Ramakrishnan Nagarajan | last post by:
Hi, I have two checkboxes in each row of a grid. One for Modify and another one for View. If I click Modify the View should get automatically checked and should be disabled. Earlier I did this in...
2
by: Javier | last post by:
Hi Everyone, I have a dynamic checkbox in a datagrid that uses the ITemplate interface and has the checkchanged event wired up. When the checkbox is checked, the event event handler that...
0
by: Keith | last post by:
I have a repeater control that contains a HeaderTemplate and an ItemTemplate. Each item contains a checkbox with an ID of chkReconciled, and the header contains a single checkbox with an ID of...
0
by: Keith | last post by:
I have a repeater control that contains a HeaderTemplate and an ItemTemplate. Each item contains a checkbox with an ID of chkReconciled, and the header contains a single checkbox with an ID of...
0
by: arunesh | last post by:
hi all I am writing an ASP.NET application using VB.NET as the code behind file. I have put a repeater control on an aspx page which is filled in from a dataset. each row of this repeater has...
12
by: uicouic | last post by:
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.