DropDownList coloring in ASP.Net
-
Hi, Is it possible to color individual items of a DropDownList box in ASP.Net? I tried the Attributes collection, but it didnt worked. If there is any other way to do the same, i would be interested in knowing. ( even if it is in javascript no problem !) I will elaborate what i actually want--- If i have some items in my DropDownList as--- ABC XYZ PQR ASDFG SDFKJ These entries will be in some logical groups. Accordingly i want to color those entries with different back colors for items in one group-- For example, ABC, XYZ will go with back color = Red, PQR with back color = Blue, etc...
"A good programmer is someone who looks both ways before crossing a one-way street." -- Doug Linder
Anant Y. Kulkarni
-
Hi, Is it possible to color individual items of a DropDownList box in ASP.Net? I tried the Attributes collection, but it didnt worked. If there is any other way to do the same, i would be interested in knowing. ( even if it is in javascript no problem !) I will elaborate what i actually want--- If i have some items in my DropDownList as--- ABC XYZ PQR ASDFG SDFKJ These entries will be in some logical groups. Accordingly i want to color those entries with different back colors for items in one group-- For example, ABC, XYZ will go with back color = Red, PQR with back color = Blue, etc...
"A good programmer is someone who looks both ways before crossing a one-way street." -- Doug Linder
Anant Y. Kulkarni
-
Hi, Is it possible to color individual items of a DropDownList box in ASP.Net? I tried the Attributes collection, but it didnt worked. If there is any other way to do the same, i would be interested in knowing. ( even if it is in javascript no problem !) I will elaborate what i actually want--- If i have some items in my DropDownList as--- ABC XYZ PQR ASDFG SDFKJ These entries will be in some logical groups. Accordingly i want to color those entries with different back colors for items in one group-- For example, ABC, XYZ will go with back color = Red, PQR with back color = Blue, etc...
"A good programmer is someone who looks both ways before crossing a one-way street." -- Doug Linder
Anant Y. Kulkarni
Hi Anant, this is the code which solve your problem. private void Page_Load(object sender, System.EventArgs e) { if (!IsPostBack) { strConn="Data Source=localhost;uid=sa;pwd=;Initial Catalog=northwind"; mycn = new SqlConnection(strConn); myda = new SqlDataAdapter ("Select * FROM CategoryTable ", mycn); ds = new DataSet(); myda.Fill (ds,"Table"); DropDownList1.DataSource =ds.Tables [0] ; DropDownList1.DataTextField =ds.Tables[0].Columns["CategoryName"].ColumnName.ToString(); DropDownList1.DataValueField =ds.Tables[0].Columns["CategoryId"].ColumnName.ToString(); DropDownList1.DataBind () ; for (int i =0;i < DropDownList1.Items.Count ;i++ ) { DropDownList1.Items[i].Attributes.Add("style", "color:" + ds.Tables[0].Rows[i]["CategoryColor"].ToString () ); } } } Hope this will solve your problem. please let me know if any problem arise bye
-
Hi Anant, this is the code which solve your problem. private void Page_Load(object sender, System.EventArgs e) { if (!IsPostBack) { strConn="Data Source=localhost;uid=sa;pwd=;Initial Catalog=northwind"; mycn = new SqlConnection(strConn); myda = new SqlDataAdapter ("Select * FROM CategoryTable ", mycn); ds = new DataSet(); myda.Fill (ds,"Table"); DropDownList1.DataSource =ds.Tables [0] ; DropDownList1.DataTextField =ds.Tables[0].Columns["CategoryName"].ColumnName.ToString(); DropDownList1.DataValueField =ds.Tables[0].Columns["CategoryId"].ColumnName.ToString(); DropDownList1.DataBind () ; for (int i =0;i < DropDownList1.Items.Count ;i++ ) { DropDownList1.Items[i].Attributes.Add("style", "color:" + ds.Tables[0].Rows[i]["CategoryColor"].ToString () ); } } } Hope this will solve your problem. please let me know if any problem arise bye
Hi,
jitu gupta wrote:
DropDownList1.Items[i].Attributes.Add("style", "color:" + ds.Tables[0].Rows[i]["CategoryColor"].ToString () );
Thats what i wanted. Thanks.
"A good programmer is someone who looks both ways before crossing a one-way street." -- Doug Linder
Anant Y. Kulkarni