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("bgco lor", "red");
Dropdownlist1.Items.Insert (0, liAux);
Dropdownlist1.Items[0].Tex t = Dropdownlist1.Items[0].Tex t +
liAux.Attributes.Count.ToS tring() +
liAux.Attributes["bgcolor" ].ToString ();
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ------
the result HTML code is the following:
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ------
<select name="Dropdownlist1" onchange="__doPostBack('Dr opdownlist 1','')" 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('Dr opdownlist 1','')" language="javascript" id="Dropdownlist1" class="cbGeoGreen11">
<option value="0" bgcolor="red">TEST1red</op tion>
</select>
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ------
I have tried with CssClass with the same result.
Could anyone help me with this trouble?
Thanks in advance.
--------------------------
liAux = new ListItem("TEST", "0");
liAux.Attributes.Add("bgco
Dropdownlist1.Items.Insert
Dropdownlist1.Items[0].Tex
liAux.Attributes.Count.ToS
liAux.Attributes["bgcolor"
--------------------------
the result HTML code is the following:
--------------------------
<select name="Dropdownlist1" onchange="__doPostBack('Dr
<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('Dr
<option value="0" bgcolor="red">TEST1red</op
</select>
--------------------------
I have tried with CssClass with the same result.
Could anyone help me with this trouble?
Thanks in advance.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
it works..then u jst said..it does not set the bg color...
so how did u fix it...
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>
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>
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. DropDownLi st
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:DropD ownList>
to:
<select id="dlTEST" runat="server" DataValueField="ID_TEST" DataTextField="D_TEST" DataMember="Table" DataSource="<%# dsTEST %>" onchange="__doPostBack('dl ADM2','')" language="javascript" class="cbGeoGreen11"></sel ect>
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.Select edIndex].V alue
and finally, my target that was to distinguish some items of the list was solved with the following:
ListItem li = dlTEST.Items.FindByValue(V ALUE);
if (li != null) li.Attributes.Add("class", "opDarkGreen11");
I hope that this will be helpful for other users.
thanks for your help.
Second,
praneetha, I solved this with the following:
I changed
System.Web.UI.WebControls.
by
System.Web.UI.WebControls.
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"><
to:
<select id="dlTEST" runat="server" DataValueField="ID_TEST" DataTextField="D_TEST" DataMember="Table" DataSource="<%# dsTEST %>" onchange="__doPostBack('dl
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.Select
and finally, my target that was to distinguish some items of the list was solved with the following:
ListItem li = dlTEST.Items.FindByValue(V
if (li != null) li.Attributes.Add("class",
I hope that this will be helpful for other users.
thanks for your help.
Thank you acperkins.
glad you could solve it.
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
See here:
Can I get a grade changed?
https://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/help.jsp#hi18
ASKER