3

I have a dropdownlist with a disabled initial value "Please select value" and when I click it, it appears again as one of the selections. Is there a way to remove this such that I will only see my items, and not see "Please select value" from the choices of selection?

    <asp:DropDownList ID="ddlRole" runat="server" AppendDataBoundItems="true" Width="115">
         <asp:ListItem Selected="True" Text="Please select value" Value="" disabled />
         </asp:DropDownList>

2 Answers2

1

Since you do not have autopostback = true, changing the selection wont take you to the server side where you can remove the first ListItem. You can do it on client side using javascript or jQuery.

HTML

<asp:DropDownList onclick="changeFun(this);" ID="ddlRole" runat="server" AppendDataBoundItems="true" Width="115">

Javascript

function changeFun(ddlRole ){         
    if(ddlRole.options[0].text == "Please select value")
        ddlRole.remove(0);
}  
Adil
  • 146,340
  • 25
  • 209
  • 204
  • Aw there's no OnChange in my IntelliSense. :( but thanks for trying! –  Mar 27 '14 at 07:12
  • You do not always get everything with IntelliSense, give it a try, onchange is javascript event, http://www.developphp.com/view_lesson.php?v=823 – Adil Mar 27 '14 at 07:14
  • Thanks! But ddlRole "does not exist in the current context"? I'm missing something. –  Mar 27 '14 at 07:32
  • ddlRole is id of dropdownlist you have, did you put javascript code in script tags? – Adil Mar 27 '14 at 07:35
  • Do you have script and drop down in same page – Adil Mar 27 '14 at 07:39
  • Yes I am very sure i put right before '< asp:DropDownList / >' –  Mar 27 '14 at 07:41
  • Try putting the script tag just before the closing body i.e. '

    ' tag

    – Adil Mar 27 '14 at 07:43
  • Ok no more errors. But still shows "Please select value", cannot be highlighted (since it's disabled), but still shows (after click on dropdown). :( –  Mar 27 '14 at 07:47
  • It will show when you click it, you have to select some other option to remove it – Adil Mar 27 '14 at 07:49
  • To remove it on click you would need onclick instead of onchange, also check my updated code – Adil Mar 27 '14 at 07:51
  • It works! Thanks. Now both "Please select value" are gone. Although I'd like the initial value to stay there + my items, it's still better than two "Please select value" showing. :) –  Mar 27 '14 at 07:53
  • Can I have the same code if I put ddlRole in a GridView? @Adil –  Mar 27 '14 at 08:01
  • Yes, with little modification, check the updated answer. – Adil Mar 27 '14 at 09:02
1
<asp:DropDownList ID="ddlRole" runat="server" AppendDataBoundItems="true" Width="115" onclick="removeFirst()">
    <asp:ListItem Selected="True" Text="Please select value" Value="plsselect"  />
    <asp:ListItem>First Item</asp:ListItem>
    <asp:ListItem>Second Item</asp:ListItem>
</asp:DropDownList>

And in Javascript

<script>
    function removeFirst() {
        var select = document.getElementById('<%= ddlRole.ClientID %>');

        for (i = 0; i < select.length; i++) {
            if (select.options[i].value == 'plsselect') {
                select.remove(i);
            }
        }
    }
</script>
pravprab
  • 2,301
  • 3
  • 26
  • 43
  • "Microsoft JScript runtime error: Unable to get value of the property 'length': object is null or undefined" at i < select :( ... Thanks for suggesting! –  Mar 27 '14 at 07:50
  • 1
    It may be because of the wrong client id. Updated my answer – pravprab Mar 27 '14 at 07:52