views:

1809

answers:

5

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.

shawn

A: 

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>
Phaedrus
A: 

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.

Mayo
+1  A: 

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

See: http://www.htmlref.com/reference/appa/tag%5Foption.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>
Mark Redman
A: 

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.

Bubacarr Demba
He wants the disabled elements to be visible just non-selectable, so not sure this will work.
Adam Fox
+4  A: 

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 contdition] )
    {
        item.Attributes.Add( "disabled", "disabled" );
    }
}

This will render your disabled elements greyed out.

Adam Fox