Subscribe to Subscribers
Talk to Webmaster Tony John


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

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


Posted Date:     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.


    Next Resource: Shopping Cart,Datatable in Session
    Previous Resource: Web Services
    Return to Resources
    Post New Resource
    Category: ASP.NET/Web Applications


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Adding style to dropdown item  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.