Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello All,

I am binding ListBox in code behind page using data from database. I have set its selection mode to be multiple. However I have to inhibit Listbox from getting selected more that 3 values.

I spent a lots of time on google but no use, Please help me out.

Thanks,
Posted
Comments
Sergey Alexandrovich Kryukov 17-Sep-14 18:38pm    
Why doing so?
—SA
NAPorwal(8015059) 17-Sep-14 18:46pm    
On the basis of user attributes, I have to restrict them while selecting items from the ListBox. That's why need to do so.
Sergey Alexandrovich Kryukov 17-Sep-14 19:16pm    
This is like saying: "I need to restrict number of selected items in ListBox, because I have to restrict them while selecting items from the ListBox". if you don't want to answer, don't. I suggested you a reasonable solution, please see.
—SA
NAPorwal(8015059) 17-Sep-14 19:31pm    
Yes, I am going through those only, I hope it's going to work.

Well, if I look at Google with the search term asp.net listbox multiple selection maximum[^], the first few post themselves are promising and directive enough. Please have a look and show us what you have tried.

If you are still stuck, come back and someone will try to help you with your issue.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-Sep-14 18:38pm    
Manas,

This information could be useful, but, unfortunately, this is not a solution to the OP's problem. Showing Google queries cannot be adequate in all cases, otherwise this forum would not be needed.
I am sure that real solution is the different UI design. I suggested much more reasonable design and motivated it (as well as the problems of OP's idea) in Solution 2, please see.

—SA
I would strongly advise you to review your UI design instead. This control is really designed to represent arbitrary selection from 0 to N items, where N is the total number of items. When you try to break this design, the component will strongly resist. Worse, if you succeed, it will be way too confusing to the users who usually expect default and reasonable behavior.

I think at least one more reasonable design is nearly obvious. Instead of having a list box, have a set of combo boxes. Even better, it would be ideal to use an instance of the class System.Web.UI.WebControls.TreeView with combo-boxes on nodes. All you would need is to set the property ShowCheckBoxes and use the property CheckedNodes:

Please see:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.treeview%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.treeview.showcheckboxes%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.treeview.checkednodes%28v=vs.110%29.aspx[^].

Now, you want to limit the number of checks. You will need to handle the event TreeNodeCheckChanged. Create some other control and display the checking status, for example "0 of 3 values checked", "1 of 3 values checked"… "3 of 3 values checked". It will provide the user with some feedback, it will become apparent that limited number of checks would be allowed. Always allow unchecking, but block checking more nodes above your limit.

—SA
 
Share this answer
 
v2
Comments
NAPorwal(8015059) 22-Sep-14 14:25pm    
Thanks your solution really worked, apart from this I also learnt many new things. Thanks again :)
Sergey Alexandrovich Kryukov 22-Sep-14 14:30pm    
This is my please to help someone who can really get the ideas and make thing working!
Good luck, call again.
—SA

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