5 Replies - 1377 Views - Last Post: 24 September 2012 - 11:56 PM Rate Topic: -----

#1 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Restricting the selection of Checkboxes

Posted 14 September 2012 - 12:12 AM

I have a checkbox list that contains 12 checkboxes.I want the user to select only 4 checkboxes. Is there any code in ASP.NET that does the same?
Please help as this is only my 2nd week in ASP.NET.

Thank you :)
Is This A Good Question/Topic? 0
  • +

Replies To: Restricting the selection of Checkboxes

#2 November-06  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 391
  • Joined: 04-January 11

Re: Restricting the selection of Checkboxes

Posted 14 September 2012 - 01:38 AM

Do you want to validate the number of checked checkbox on button click? In that case, in your button_click event,

int intCheckedItems = 0;
foreach (ListItem li in cbl.Items)
{
   if (li.Selected)
      intCheckedItems += 1;
}

if (intCheckedItems>4)
   //Prompt user that he can only select four items


Was This Post Helpful? 1
  • +
  • -

#3 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Restricting the selection of Checkboxes

Posted 14 September 2012 - 02:29 AM

View PostNovember-06, on 14 September 2012 - 02:08 PM, said:

Do you want to validate the number of checked checkbox on button click? In that case, in your button_click event,

int intCheckedItems = 0;
foreach (ListItem li in cbl.Items)
{
   if (li.Selected)
      intCheckedItems += 1;
}

if (intCheckedItems>4)
   //Prompt user that he can only select four items



Thank you very much for your reply. When I tried doing this, I found out that there is no command in ASP.NET to prompt the user like a java alertbox. Can you please explain what you meant by prompt the user? Thank you.
Was This Post Helpful? 0
  • +
  • -

#4 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 262
  • View blog
  • Posts: 1,461
  • Joined: 07-April 08

Re: Restricting the selection of Checkboxes

Posted 14 September 2012 - 06:14 AM

You will either have to A: register javascript to alert the user, or B use a validation summary, and fire a new validation error when it gets above 4 checkboxes.
Was This Post Helpful? 1
  • +
  • -

#5 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Restricting the selection of Checkboxes

Posted 16 September 2012 - 08:14 PM

View Postrgfirefly24, on 14 September 2012 - 06:44 PM, said:

You will either have to A: register javascript to alert the user, or B use a validation summary, and fire a new validation error when it gets above 4 checkboxes.


Thank you very much for your reply.I guess I got my answer now. Thank you :)
Was This Post Helpful? 0
  • +
  • -

#6 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Restricting the selection of Checkboxes

Posted 24 September 2012 - 11:56 PM

Thank you everyone for all your help. I managed to find the solution by using a function as follows :)

function chkCount(obj) {
           if (obj.checked == true) {
               if (document.getElementById('<%=hiddenChkCount.ClientID %>').value >= 8) {
                   alert('You cannot select more than 8 Magazines.');
                   obj.checked = false;
               }
               else {
                   document.getElementById('<%=hiddenChkCount.ClientID %>').value = parseInt(document.getElementById('<%=hiddenChkCount.ClientID %>').value) + 1;
               }
           }
           else {
               document.getElementById('<%=hiddenChkCount.ClientID %>').value = parseInt(document.getElementById('<%=hiddenChkCount.ClientID %>').value) - 1;
           }
       }  


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1