页面中有多个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];
}
}
}
}
}
该怎么做呢?
下面是我的代码:
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];
}
}
}
}
}
label总能加上背景色了吧
这样报错吗
试下能这样吗
rbl.Items[i].Properties.Text = ItemText[j];
http://www.cnblogs.com/insus/archive/2012/04/01/2428761.html
明白了,相信你的问题也不是问题了。
{
RadioButtonList rbl = sender as RadioButtonList;
foreach (ListItem item in rbl.Items)
{
if (item.Selected)
{
item.Attributes.Add("style","background-color: red;");
}
}
}