Click here to Skip to main content
11,494,943 members (59,179 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
How to show alert message when none of the checkbox items are selected in asp.net
Posted 2-Oct-12 2:47am
cunny301
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Two ways:
1. Client side:
On page submit, button click have a defined client side onclick. In it, loop through the checkboxes and see if any is selected. If none, show an alert message and avoid postback

2: Server side:
Loop through checkboxes and see if any checkbox checked. If so, continue or else display the message.

Try.
  Permalink  
Comments
cunny at 2-Oct-12 8:30am
   
Yeah my requirement is like this...
I have a checkbox in header template as well as in item template..
If we select checkboxes on header template (Select All) it must select all checkboxes and if din't select any invidual checkbox it must show a alert message saying that..(Please select atleast one checkbox)
Sandeep Mewara at 2-Oct-12 8:34am
   
Good. So, go ahead and try option 1.
Sergey Alexandrovich Kryukov at 2-Oct-12 21:21pm
   
Right, a 5.
See also the link by Dylan Morley -- quite an elegant solution.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

A jQuery + jQuery.validate method here,

http://stackoverflow.com/questions/3035634/jquery-validate-check-at-least-one-checkbox[^]


jQuery only, with click handler - so your submit button is only activated when at least one is selected

http://css-tricks.com/snippets/jquery/test-if-at-least-one-checkbox-is-checked/[^]
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 2-Oct-12 21:19pm
   
Best idea so far, a 5.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Dim str As String=""
For(i As integer=0 To chkList.Items.count-1)
if(chkList.Items(i).checked==True) Then
str+=chkList.Items(i).value
End If
Next

if(str==" ")
MsgBox('Please Select Item')
End If

sorry it's in VB.net
  Permalink  
v4
Comments
Sandeep Mewara at 2-Oct-12 8:34am
   
Not a web solution.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Try this
function check()
{
var a=document.getElementsByName("checkbox"
var j=0
for(i=0;i<=a.length;i++)
{
if(a[i].checked==true)
{
j=j+1;
}
}
if (j==0)
{
alert("please select checkbox")
}
}
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Can you use a validation summary and on the properties, set the 'ShowMessageBox' option to 'true'
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Dnyaneshwar@Pune 692
1 Sergey Alexandrovich Kryukov 350
2 CHill60 318
3 Sascha Lefèvre 280
4 Nakul Chaudhari 236
0 Sergey Alexandrovich Kryukov 10,297
1 OriginalGriff 8,821
2 Sascha Lefèvre 3,864
3 Maciej Los 3,412
4 Richard Deeming 2,500


Advertise | Privacy | Mobile
Web03 | 2.8.150520.1 | Last Updated 2 Oct 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100