Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make drop down list item unselectable

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.

like image 303
ErnieStings Avatar asked Sep 04 '09 13:09

ErnieStings


People also ask

What is correct option for making drop-down list?

A select box also called a drop-down box provides an option to list down several options in the form of a drop-down list, from where a user can select one or more options. The <select> tag is used to create a drop-down list in HTML, with the <option> tag.

How to disable item in DropDownList in asp net c#?

Data. SqlClient; Once the DropDownList is populated with the data, a default item is inserted at the first position and is selected. Then the default item is disabled by setting the 'disabled' attribute of the item.

How to make drop-down list not selectable?

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.


5 Answers

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.

like image 123
Adam Fox Avatar answered Oct 19 '22 03:10

Adam Fox


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>
like image 34
Mark Redman Avatar answered Oct 19 '22 02:10

Mark Redman


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>
like image 4
Phaedrus Avatar answered Oct 19 '22 03:10

Phaedrus


You could try this

myDropDownList.Items.FindByValue("ReportValue").Attributes.Add("disabled", "disabled");
like image 2
Victor Caldas Avatar answered Oct 19 '22 02:10

Victor Caldas


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.

like image 1
Samy Alihamad Avatar answered Oct 19 '22 02:10

Samy Alihamad