Link to home
Create AccountLog in
Avatar of 2much4u
2much4u

asked on

Change Items styles in a DropDown Combo

Hello, I want to change the style of some items of a drop down list web control from the server side with the followintg example code:

------------------------------------------------------------------------------------------------------
liAux = new ListItem("TEST", "0");
liAux.Attributes.Add("bgcolor", "red");
Dropdownlist1.Items.Insert(0, liAux);
Dropdownlist1.Items[0].Text = Dropdownlist1.Items[0].Text +
                                                    liAux.Attributes.Count.ToString() +
                                                    liAux.Attributes["bgcolor"].ToString();
------------------------------------------------------------------------------------------------------

the result HTML code is the following:
------------------------------------------------------------------------------------------------------
<select name="Dropdownlist1" onchange="__doPostBack('Dropdownlist1','')" language="javascript" id="Dropdownlist1" class="cbGeoGreen11">
      <option value="0">TEST1red</option>

</select>
------------------------------------------------------------------------------------------------------

where's the bgcolor attribute of the option item???? but as you can see the text of the item contains the attribute so the item have the attribute... so the HTML should look like this:

------------------------------------------------------------------------------------------------------
<select name="Dropdownlist1" onchange="__doPostBack('Dropdownlist1','')" language="javascript" id="Dropdownlist1" class="cbGeoGreen11">
      <option value="0" bgcolor="red">TEST1red</option>

</select>
------------------------------------------------------------------------------------------------------

I have tried with CssClass with the same result.

Could anyone help me with this trouble?

Thanks in advance.

ASKER CERTIFIED SOLUTION
Avatar of praneetha
praneetha

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of 2much4u
2much4u

ASKER

a bug? do you really think that? everything is working fine in my app, and I've tested this example in two different servers.
it works..then u jst said..it does not set the bg color...

so how did u fix it...
Avatar of Anthony Perkins
2much4u,

Suggest you take a look at the EE Guidelines regarding grading standards.
What's the right grade to give?
https://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/help.jsp#hi73

And specifically this section:
<quote>
The use of a C in a vindictive manner is likely to be changed by a Moderator. You may not like the answer you get, and in some cases, and you may not like the way it is delivered, but if it is deemed to be accurate, no less than a B is an acceptable grade.
</quote>
Avatar of 2much4u

ASKER

First of all, thanks for you comment acperkins, I should gave a B to the answer. How can I change it?

Second,

praneetha, I solved this with the following:

I changed

System.Web.UI.WebControls.DropDownList

by

System.Web.UI.WebControls.HtmlSelect

and making some minor changes in my HTML code, changing this:

<asp:DropDownList id=dlTEST runat="server" DataSource="<%# dsTEST %>" DataMember="Table" DataTextField="ID_TEST" DataValueField="ID_TEST"></asp:DropDownList>

to:

<select id="dlTEST" runat="server" DataValueField="ID_TEST" DataTextField="D_TEST" DataMember="Table" DataSource="<%# dsTEST %>" onchange="__doPostBack('dlADM2','')" language="javascript" class="cbGeoGreen11"></select>

in CodeBehind everything works in the same way, excepts the way of get the value of the selected item that changes from

dlTEST.SelectedValue;

to:

dlTEST.Items[dlTEST.SelectedIndex].Value

and finally, my target that was to distinguish some items of the list was solved with the following:

ListItem li = dlTEST.Items.FindByValue(VALUE);
if (li != null) li.Attributes.Add("class", "opDarkGreen11");


I hope that this will be helpful for other users.
thanks for your help.
Thank you acperkins.

glad you could solve it.

>>First of all, thanks for you comment acperkins, I should gave a B to the answer. How can I change it?<<
See here:
Can I get a grade changed?
https://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/help.jsp#hi18