Disable multiple items from DropDownList. Someone need to check code!

dejandejanovic
dejandejanovic used Ask the Experts™
on
Hello,
I would like to disable a specific items from DropDownList. I have below code but it does not working. So, need someone to told me what I'm doing wrong. DropDownList have 5 items.
And, I'm getting error:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Protected Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        Dim X As Integer = 0 <> 1
        DropDownList1.Items(X).Enabled = False

    End Sub

Open in new window


But, if I add a different value for X, then it disable 1st and 2nd item.
Protected Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        Dim X As Integer = 0 or 1
        DropDownList1.Items(X).Enabled = False

    End Sub

Open in new window


My goal is if I would have a 365 items in DropwDownList to disable items for example between 120 and 150.

Thank you in advance for help to anyone.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You cannot disable specific items in a dropdownlist. You have to do that in the code. If the user selects an item that he is not suppose to do, you do not let him and you pop up a message to him stating that he cannot select this item.
May be you can write some javascript that checks selected item, compares with alllowed selection items and resets dropdown selected option. You can do this by maintainaining 1 hidden input control that tracks Allowable selection items in comma separated manner.

example syntax for javascript - onchange
http://forums.asp.net/t/1224793.aspx/1?Javascript+for+onchange+event+in+asp+dropdownlist

Author

Commented:
I have found a solution for me.
Dim X as integer
For x = 0 to 4
If DropDownList1.item(x).text.Contains("May") then
msgbox("yes")
else
msgbox("no")
next

So, maybe this will help also to other users.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial