Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to show alert message when none of the checkbox items are selected in asp.net
Posted

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.
 
Share this answer
 
Comments
cunny 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 2-Oct-12 8:34am    
Good. So, go ahead and try option 1.
Sergey Alexandrovich Kryukov 2-Oct-12 21:21pm    
Right, a 5.
See also the link by Dylan Morley -- quite an elegant solution.
--SA
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/[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Oct-12 21:19pm    
Best idea so far, a 5.
--SA
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
 
Share this answer
 
v4
Comments
Sandeep Mewara 2-Oct-12 8:34am    
Not a web solution.
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")
}
}
 
Share this answer
 
v2
Can you use a validation summary and on the properties, set the 'ShowMessageBox' option to 'true'
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900