Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DropDownList Remove "Please select value" from selection upon clicking?

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>

1 Answers

<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>
like image 140
pravprab Avatar answered Apr 19 '26 23:04

pravprab