Click here to Skip to main content
15,880,891 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello guys,

I need some assistance.
I have a checkboxlist having cities. And I want to do something in it.
When I select 3 cities from the CheckBoxList then after selection, it must automatically disable the remaining items.
Means:
In CheckBoxList there are 5 items. If any 3 of the 5 items are selected, then remaining 2 must automatically get disabled.



You can also see this site.
www.99acres.com(go to the advanced search)
There u can find this functionality.

Please some body help me.
I have got stuck in between my programming.
Posted
Updated 26-Aug-11 18:01pm
v4
Comments
Herman<T>.Instance 26-Aug-11 6:39am    
show your code!
BillWoodruff 26-Aug-11 8:09am    
Have you considered the possibility that the site you mention is probably using a custom list control, not an ASP.NET control ?
[no name] 26-Aug-11 8:34am    
I believe the OP wants to achieve the functionality similar to the one in the site he mentions.
nit_singh 27-Aug-11 11:58am    
@leonidasvijay if my solution works for you then mark it as answer

you can apply a simple logic to make items disabled if selected item count reach to a limit.

I tried and I found a solution. have a look on it

C#
<script language="javascript" type="text/javascript">
        function disableAfterSelecton(ctrl) {
            var disableItemsAfetrSelection = ctrl.getAttribute('maxSelectedCount', 0);
            var inputRefArray = ctrl.getElementsByTagName('input');

            if (getSelectedCount(ctrl) == disableItemsAfetrSelection) {
                //checking if the selected item count reached to the limit, then disbled remaining
                for (i = 0; i < inputRefArray.length; i++) {
                    if (!inputRefArray[i].checked) makeItemEnableDisable(inputRefArray[i], false);
                }
            }
            else if (getSelectedCount(ctrl) < disableItemsAfetrSelection) {
                //checking if the selected item count is below the limit then again enable the remaining items
                for (i = 0; i < inputRefArray.length; i++) {
                    if (!inputRefArray[i].checked) makeItemEnableDisable(inputRefArray[i], true);
                }
            }
        }

        // function to retun the selected items count
        function getSelectedCount(ctrl) {
            var inputRefArray = ctrl.getElementsByTagName('input');
            var checkedCount = 0;
            for (i = 0; i < inputRefArray.length; i++) {
                if (inputRefArray[i].checked) ++checkedCount;
            }
            return checkedCount;
        }

        function makeItemEnableDisable(item, isEnabled) {
            if (!isEnabled)
                item.disabled = 'disabled';
            else
                item.disabled = '';
        }
    </script>



here is the CheckBoxList code

<asp:CheckBoxList ID="chkItems" runat="server" maxSelectedCount="3" onclick="return disableAfterSelecton(this);">


Here maxSelectedCount is the custom property which you can decide based on your requirement. This is my idea, you can make the code looks better.

Thanks
 
Share this answer
 
v3
Comments
Tech Code Freak 28-Aug-11 5:37am    
My 5 that you found the solution on your own!
You can also accept the correct answers which were helpful to you.
Hi,

As far as I know, the Disabled property stands for the whole checkboxlist, not for its items.

This means you cannot disable some items of the checkboxlist ; you can only disable the whole control.

Regards.
 
Share this answer
 
Comments
leonidasvijay 26-Aug-11 6:34am    
Yes i know,but we can do this.we can disabled the not selected item from the list.
You cal also see this site.
www.99acres.com(go to the advanced search)
there u can find this functionality.
[no name] 26-Aug-11 8:34am    
I believe the OP wants to achieve this functionality.
I think you might need to do something with
manage_optionlist
javascript function called by checkbox. If you view the source of the page you would find following code for the checkbox,

Select Upto 5 Property Types</span></label><div class="lf scrollbox" id='proptype_scroll'><input name="property_type[]" type="checkbox" id="Residential Apartment" value="1" class="chbx" onclick="return manage_optionlist('|proptype');">


I couldn't find the function in the source code but I would guess it has been declared somewhere in the site, you would need to find that.

:)
 
Share this answer
 
you can view source their website, you will get some hint.
 
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