Reputation: 337
I have a dropdown list in my asp.net page which has a list of 10 items. I want to hide some of these items from the dropdown so that the user cannot select them. However, I want to initialise the dropdown control's value with one of these hidden items. If the user doesn't touch the dropdown, then this value will be valid even though it is hidden in the dropdown options. On the other hand, if the user opens the dropdown then he cannot select a value that was hidden.
How would you go about this? I tried setting ddl.Items[0].Enabled = false, but then I cannot initialise the control to this hidden value. ie if it is hidden, the item cannot be selected not even programatically...
Upvotes: 0
Views: 6151
Reputation: 460068
You could try this code:
ListItem i = myDropDownList.Items.FindByValue("1");
i.Attributes.Add("style", "color:gray;");
i.Attributes.Add("disabled", "true");
i.Value = "-1";
http://forums.asp.net/t/1132654.aspx
Upvotes: 3