user3435955
user3435955

Reputation:

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>

Upvotes: 3

Views: 2166

Answers (2)

Adil
Adil

Reputation: 148110

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);
}  

Upvotes: 1

pravprab
pravprab

Reputation: 2293

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

Upvotes: 1

Related Questions