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


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.


Comments

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