1

I have in dropdownlist some values, (for example 1, 2, 3) and I want to hide one of them (for example 2), then when i click on dropdownlist I want to see only 1 and 3 values...

I use this code, but it doesnt work...

dropDownList.Items[0].Attributes.Add("style", "display: none");

this solution doesnt work too...

dropDownList.Items[0].Attributes.Add("style", "visibility: hidden");

but when I for example setting color with this solution, it works...

Do somebody know how to set it in C#?

Later I want to unhide this value and I want to see all values (1, 2, 3)

Thx.

weto
  • 69
  • 1
  • 1
  • 12

4 Answers4

3

I think you should try removing the value

ListItem removeItem=dropDownList.Items.FindByValue("1");
dropDownList.Items.Remove(removeItem);
COLD TOLD
  • 13,513
  • 3
  • 35
  • 52
3

How about do disable it? The code should be something like this:

dropDownList.Items[0].Enabled = false;

Else you can hide it with jQuery:

$("#yourDropDownListId option[value='0']").hide();
brothers28
  • 1,196
  • 17
  • 23
0

I'll go with COLD TODD's answer. Hiding items of a select box using css is a browser dependent behavior - it may work on some browsers; it may not work on others as expected.

I assume you are talking about a small number of items in a list. You can cache it in the viewstate always. Use a NameValueCollection to store such items.

I haven't tested in all the browsers. In IE10, you cannot hide items at all. In chrome, you cannot hide the first item at all, no matter what. You can play with the example on jsfiddle:

<select id='myoptions'></select>
<br/>
<br/>Hide:
<br/>
<div class="form-inline">
    <select id="ctrls"></select>
    <input type="button" class="btn btn-default" value="Go" id="btnhide" />
</div>

onLoadScript:

var data = {
    'foo1': 'bar1',
        'foo2': 'bar2',
        'foo3': 'bar3',
        'foo4': 'bar4',
        'foo5': 'bar5'
};
$(function () {
    var $sel = $('#myoptions');
    var $sel2 = $('#ctrls');
    $.each(data, function (idx, val) {
        var opt = document.createElement('option');
        opt.value = val;
        opt.innerHTML = idx;
        $sel.append(opt);
        $sel2.append($(opt).clone());
    });
    $('#btnhide').click(function () {
        var val = $sel2.val();
        $sel.find('option').show();
        $sel.find('option[value=' + val + ']').hide();
    });
});
deostroll
  • 11,661
  • 21
  • 90
  • 161
0

Assuming you are using an ASP dropdownlist, you just have to add Enabled="False" to whichever one you want to hide.

<asp:DropDownList ID='DDL' runat="server" >  
    <asp:ListItem>1</asp:ListItem>
    <asp:ListItem Enabled="False">2</asp:ListItem>
    <asp:ListItem>3</asp:ListItem>
 </asp:DropDownList>