Wednesday 11 January 2012

asp.net dropdownlist Header value disabled to select

we  have a list of 10-50 items that will be in a dropdown list. Users need to select an Item to perform a function.
Each Item has a Heading that it's sorted on, 10 items under 1 heading (example). and heading not selected


  <asp:DropDownList ID="DropDownList1" runat="server"
            onselectedindexchanged="DropDownList1_SelectedIndexChanged">
            <asp:ListItem>ww</asp:ListItem>
            <asp:ListItem>aa</asp:ListItem>
            <asp:ListItem>bb</asp:ListItem>
            <asp:ListItem>cc</asp:ListItem>
            <asp:ListItem>dd</asp:ListItem>
        </asp:DropDownList>


In aspx.cs


 protected void Page_Load(object sender, EventArgs e)
    {
        string str = "aa";
        ListItem i = DropDownList1.Items.FindByValue(str);
        i.Attributes.Add("style", "color:gray;");
        i.Attributes.Add("disabled", "true");

    }