页面中有多个RadioButtonList控件,我想让这些控件的SelectedIndexChanged事件都绑定到同一个方法(protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e))中,给选中项(ListItem)的text更换背景颜色;
该怎么做呢?
下面是我的代码:
List<string> ItemText = new List<string>(); 
        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        { 
            RadioButtonList rbl = sender as RadioButtonList;
            //string str= r.Items[0].Text;  
                foreach (ListItem item in r.Items)//我想记录下来每个控件的初始ListItem的Text;但是这样很明显不对
                {
                    ItemText.Add(item.Text);
                }   
            for (int i = 0; i < rbl.Items.Count; i++)
            {
                if (rbl.Items[i].Selected)
                {
                    rbl.Items[i].Text = "<font style=\"color:Red\">" + rbl.Items[i].Text + "</font>";//换背景色 
                }
                else//如果不是选中项就给他重新绑定初始数据
                {
                    for (int j = 0; j < ItemText.Count; j++)
                    {
                        if (i==j)
                        {
                            rbl.Items[i].Text = ItemText[j];
                        }
                    } 
                }
            }
        } 

解决方案 »

  1.   

    你可以radionbutton只搞个圆框,他的text弄成空,然后旁边放label
    label总能加上背景色了吧
      

  2.   

    现在就是得到各个ItemList的text初始值做不了,背景色倒是好改。
      

  3.   

    rbl.Items[i].Text = ItemText[j];
    这样报错吗
    试下能这样吗
    rbl.Items[i].Properties.Text = ItemText[j];
      

  4.   

    参考:
    http://www.cnblogs.com/insus/archive/2012/04/01/2428761.html
    明白了,相信你的问题也不是问题了。
      

  5.   

    您好,我的页面中有很多RadioButtonList,我想为他们都绑定一个方法RadioButtonList1_SelectedIndexChanged(),为选中项添加背景色,但是这样有一个Bug,这些RadioButtonList永远只有一个项是有红色背景色的。纠结ing!我的代码如下:protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
            { 
                RadioButtonList rbl = sender as RadioButtonList;  
                foreach (ListItem item in rbl.Items)
                {
                    if (item.Selected)
                    { 
                        item.Attributes.Add("style","background-color: red;");
                    } 
                }
            }