make drop down list item unselectable

Viewed 86574

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

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.

You could try this

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

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.

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).

Related