Rami Far
Rami Far

Reputation: 404

First item of dropdownlist item non selectable asp.net

I want to make first item of dropdownlist non selectable following is my code.

<asp:DropDownList ID="IssueList" runat="server" Height="24px" Style="margin-left: 0px">
     <asp:ListItem>Select One</asp:ListItem>
     <asp:ListItem>Bug</asp:ListItem>
     <asp:ListItem>Enhancement</asp:ListItem>
     <asp:ListItem>Tasks</asp:ListItem>
     <asp:ListItem>New Feature</asp:ListItem>
</asp:DropDownList>

Please help me how to do this.

Upvotes: 0

Views: 2115

Answers (4)

Jaydip Jadhav
Jaydip Jadhav

Reputation: 12309

You could use attribute as in combination of disabled and selected. may this will help to get resolve your issue.

<asp:ListItem Selected="True" disabled="disabled">Select One</asp:ListItem>
                   ^^^^^^^         ^^^^^^^

Upvotes: 1

jekcom
jekcom

Reputation: 2095

You can also make it disabled in code behind on page load event

protected void Page_Load(object sender, EventArgs e)
{
    IssueList.Items[0].Attributes["disabled"] = "disabled";
}

also make your first item selected

                   <asp:DropDownList ID="IssueList" runat="server" Height="24px" Style="margin-left: 0px">
                         <asp:ListItem Selected="true" Value="">Select One</asp:ListItem>
                        <asp:ListItem>Bug</asp:ListItem>
                        <asp:ListItem>Enhancement</asp:ListItem>
                        <asp:ListItem>Tasks</asp:ListItem>
                        <asp:ListItem>New Feature</asp:ListItem>
                    </asp:DropDownList>

Upvotes: 2

Tien Nguyen Ngoc
Tien Nguyen Ngoc

Reputation: 1555

You can add an 'Empty' value item:

<asp:DropDownList ID="IssueList" runat="server" Height="24px" Style="margin-left: 0px">
    <asp:ListItem Selected="True"></asp:ListItem>
    <asp:ListItem>Select One</asp:ListItem>
    <asp:ListItem>Bug</asp:ListItem>
    <asp:ListItem>Enhancement</asp:ListItem>
    <asp:ListItem>Tasks</asp:ListItem>
    <asp:ListItem>New Feature</asp:ListItem>
</asp:DropDownList>

Then you would check for

if (string.IsNullOrEmpty(IssueList.SelectedValue))

Upvotes: 1

Jaimin Dave
Jaimin Dave

Reputation: 1222

You can add disabled attribte.try with this:

<asp:DropDownList runat="server" ID="id">
    <asp:ListItem Text="Test" Value="value" disabled="disabled" />
</asp:DropDownList>

Upvotes: 2

Related Questions