hmk
hmk

Reputation: 969

making some items unselectable in dropdownlist using asp.net?

Dropdownlist i binded the values but in that dropdownlist just like group means(employee,nonemployee) so that items value is empty(""), so i can use databound event split the two fileds ,that two fields i can apply the color and underline and bould and user doesnot select that fields , so pls see the below code and modify this code.

protected void ddlconsultant_DataBound(object sender, EventArgs e) { foreach (ListItem item in ((DropDownList)sender).Items) {

string r = item.Value; if (r == "") {

item.Attributes.Add("style", "color:Red;font-weight:bolder"); } }

thanks hemanth

Upvotes: 3

Views: 4786

Answers (2)

Fan Yang
Fan Yang

Reputation: 31

It's probably easier to do this with server-side code, in the same place where you set the colour of list items:

item.Attributes.Add("style", "color:Red;font-weight:bolder");
item.Attributes.Add("disabled", "disabled");

This will product HTML code that looks something like this:

<option style="color:Red;font-weight:bolder" disabled="disabled">item text</option>

I know this is kinda an old question, but I've just been looking for the same information, and having just found out, thought I'd add the answer here for completeness.

Upvotes: 2

Adrian Iftode
Adrian Iftode

Reputation: 15663

I'm handling this situation on the client side, using javascript, actually jQuery

jQuery(document).ready(function () {
        $("[id*=ddlConsultant] option[value='']").each(function () {
            $(this).attr("disabled", "true");
            $(this).css("color", "Red");
            $(this).css("font-weight", "bolder");
        });
    });

Upvotes: 3

Related Questions