• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

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.

0
2much4u
Asked:
2much4u
  • 4
  • 2
  • 2
1 Solution
 
praneethaCommented:
i think some bug with web server control...use html drop downlist and make it run at server...
0
 
2much4uAuthor Commented:
a bug? do you really think that? everything is working fine in my app, and I've tested this example in two different servers.
0
 
praneethaCommented:
it works..then u jst said..it does not set the bg color...

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
praneethaCommented:
so how did u fix it...
0
 
Anthony PerkinsCommented:
2much4u,

Suggest you take a look at the EE Guidelines regarding grading standards.
What's the right grade to give?
http://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>
0
 
2much4uAuthor Commented:
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.
0
 
praneethaCommented:
Thank you acperkins.

glad you could solve it.

0
 
Anthony PerkinsCommented:
>>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?
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/help.jsp#hi18
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now