set Different colors in asp:DropDownList Items

前端 未结 3 1508
滥情空心
滥情空心 2021-01-22 10:49

I have an in my form, with 4 different options (items) What I need to do it\'s to set a different color from client side, without PostBack.

Items:

  • Selec
3条回答
  •  攒了一身酷
    2021-01-22 11:20

    Try out the following code

    In the filename.aspx.cs file...

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            System.Drawing.Color c1 = new System.Drawing.Color();
            Type t = c1.GetType();
            int row;
            foreach (System.Reflection.PropertyInfo p1 in t.GetProperties())
            {
                ColorConverter d = new ColorConverter();
                try
                {
    
                    DropDownList2.Items.Add(p1.Name);
                    for (row = 0; row < DropDownList2.Items.Count - 1; row++)
                    {
                        DropDownList2.Items[row].Attributes.Add("style",
                              "background-color:" + DropDownList2.Items[row].Value);
                    }
                }
                catch
                {
                    // Catch exceptions here if needed
                }
            }
         }
    }
    

提交回复
热议问题