Resources » .NET programming » ASP.NET/Web Applications

Adding attributes such as backgorund color, forecolor to the particular item of a dropdownlist


Posted Date: 27-Jan-2008  Last Updated:   Category: ASP.NET/Web Applications    
Author: Member Level: Gold    Points: 10


to set a background color or forecolor for a particular item of a dropdownlist based on some conditions



<H2>Setting Background color and Fore color of a particular item</H2>

To be frank if u try to set a background color or a fore color of a particular item in a web control dropdownlist based on some conditions using "Dropdownlist1.Item[0].Attributes.CssStyle.Add", the change will not reflect in the page while executing. To handle this, Use a html dropdownlist <Select id=Select1></Select>, make this as a server control by right click the control.


//html dropdownlist
Select1.Items.Add("1");
Select1.Items.Add("2");
Select1.Items.Add("3");
Select1.Items.Add("4");
Select1.Items[1].Attributes.CssStyle.Add("BACKGROUND-COLOR","green");
Select1.Items[3].Attributes.CssStyle.Add("COLOR","red");
//to execute onchange event on selected index chage [auto postback]
Select1.Attributes.Add("onchange","__doPostBack('Select1','')");

//web control dropdownlist
DropDownList1.Items.Add("0");
DropDownList1.Items.Add("1");
DropDownList1.Items.Add("2");
DropDownList1.Items.Add("3");
DropDownList1.Items.Add("4");
DropDownList1.Items[2].Attributes.CssStyle.Add("BACKGROUND-COLOR","green");
DropDownList1.Items[4].Attributes.CssStyle.Add("COLOR","red");
//This will not reflect..No color will be set for a particular item.


Try this...Sometimes u may come across this situation of handling different colors for different items of a dropdownlist.


Did you like this resource? Share it with your friends and show your love!

Responses to "Adding attributes such as backgorund color, forecolor to the particular item of a dropdownlist"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    Nagarju Prathi
    More...
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India