Tuesday, 1 October 2013

Setting the Dropdown Text which is not displayed in dropdown items in Asp.net

Many times we need to show the Text in the Dropdown but we need to stop the user from selecting it from the item list.
So here is the simple javascript to do the same.

Step 1: Page Markup

<h3>DropdownList Example</h3>
        Gender: <asp:DropDownList ID="ddlCheck" runat="server">  </asp:DropDownList>

  

Step 2: Code to bind the Dropdown

 ddlCheck.Items.Clear();
 ddlCheck.Items.Add("Select Gender");
 ddlCheck.Items.Add("Male");
 ddlCheck.Items.Add("Female");
 


Step 3: Now to Hide the 0th Item i.e "Select Gender" from the dropdownlist, write the following after binding the dropdownlist

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


Output:


No comments:

Post a Comment