make drop down list item unselectable

后端 未结 9 505
天涯浪人
天涯浪人 2020-11-30 07:26

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

相关标签:
9条回答
  • 2020-11-30 07:47

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

    0 讨论(0)
  • 2020-11-30 07:48

    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.

    0 讨论(0)
  • 2020-11-30 07:53

    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.

    0 讨论(0)
  • 2020-11-30 07:58

    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.

    0 讨论(0)
  • 2020-11-30 07:59

    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>
    
    0 讨论(0)
  • 2020-11-30 08:00

    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

    0 讨论(0)
提交回复
热议问题