38

I have a dropdownlist which has several options for generating reports. Based on the type of account the user has certain options which should be visible but not selectable (as an incentive for them to upgrade).

I was wondering if anyone knew of a way to accomplish this.

The permissions are already in place i just need assistance with making certain items unselectable.

Any help would be much appreciated.

9 Answers 9

69

Not sure if you are still looking for an answer for this?

Mark Redman's answer is great if you can define the select list in the aspx page, however if you bind the drop down list dynamically obviously you cannot.

I had success using the following to achieve the result you are after (not sure on full browser support but works in newer versions of IE)

foreach ( ListItem item in dropdownlist.Items )
{
    if ( [item should be disabled condition] )
    {
        item.Attributes.Add( "disabled", "disabled" );
    }
}

This will render your disabled elements greyed out.

Sign up to request clarification or add additional context in comments.

3 Comments

I am adding your suggested code in the Page_Load event but ListItem item (Select) that I want to be disabled is still not grayed out. Any idea why?
Does it disable the item? If it does it might be styles that need adjusting too?
You need to add the code in the databinding event handler for the drop down list control.
19

You can disable an <option> tag in an html <select>

See: http://www.htmlref.com/reference/appa/tag_option.htm

in asp.net:

<asp:DropDownList ID="MyDropDownList" runat="server">
        <asp:ListItem Text="Standard Report" Value="SR"></asp:ListItem>
        <asp:ListItem Text="Enterprise Report" Value="ER" disabled="disabled"></asp:ListItem>
    </asp:DropDownList>

1 Comment

How to do it in ASP.NET?
4

You can use a required field validator and set the initial value property to the value of the item in the drop down list you do not want selectable.

<asp:RequiredFieldValidator ID="RequiredFieldValidator" runat="server"
                        ErrorMessage="" ControlToValidate="DropDown" InitialValue="Unselectable Item"></asp:RequiredFieldValidator>

Comments

2

You could try this

myDropDownList.Items.FindByValue("ReportValue").Attributes.Add("disabled", "disabled");

Comments

1

I had this same problem and tried to use the first answer posted, but it didn't work for me. I then changed the first post to:

foreach ( ListItem item in dropdownlist.Items )
{
  if ( [item should be disabled contdition] )
  {
     item.Enabled = false;
  }
}

and it worked for me.

1 Comment

This approach removes the element from the list, and does not disable it. So it's not answering the initial question in this post.
1

Adam Fox is absolutely correct with this snippet:

foreach ( ListItem item in dropdownlist.Items )
{
    if ( [item should be disabled condition] )
    {
        item.Attributes.Add( "disabled", "disabled" );
    }
}

The only caveat is that it needs to be called in the OnDataBound event, and not the OnDataBinding event (this is too early in the lifecycle).

Comments

0

You could do this client-side with a handler that is triggered when an item is selected. Then unselect the item and/or display an error message.

Comments

-1

If this is HTML control, then its very easy to make it UNselecteable. just use the "optgroup" HTML element. e.g.

<select>
<optgroup label="Hardware"></optgroup>
<option id="1">mouse</option>
<option id="2">keyboard</option>
<option id="3">monitor</option>
<optgroup label="Software"></optgroup>
<option id="Option1">windows XP</option>
<option id="Option2">MS Office</option>
<option id="Option3">VStudio</option>
</select>

(NOTE : this works in both IE/firefox)

Thanks Sushil Jinder

2 Comments

This is not what optgroup is for.
Your comment is not helpful unless you reveal what optgroup IS for.
-3

Try this

myDropDownList.Items.FindByValue("ReportValue").Enabled = false;

This will disable the item from the list by basically not showing it in the list.

"ReportValue" = the value of the item to be disabled.

1 Comment

He wants the disabled elements to be visible just non-selectable, so not sure this will work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.