我在asp网中使用收音机RadioButtonList来创建2个单选按钮,当选择时应该改变颜色,第一个是绿色,第二个是红色,但我无法进行更改说颜色
Tldr:更改为列表项目绿色列表项目内的小圆圈,更改为列表项目2小圆圈变为红色。
也出于某种原因,它无法识别RadioButtonList1.Items中的radiobuttonlist1
这是我的HTML代码
<asp:RadioButtonList CssClass="listitemcss" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged" ID="RadioButtonList1" RepeatDirection="Horizontal" runat="server">
<asp:ListItem >
<p style="color:transparent;"> s2</p>
</asp:ListItem>
<asp:ListItem Selected="True">
<p style="color:transparent;">2</p>
</asp:ListItem>
</asp:RadioButtonList>
这是我的c#
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (RadioButtonList1.Items[0].Selected)
{
RadioButtonList1.Items[0].Attributes.Add("style", "background-color: green;");
}
if (RadioButtonList1.Items[1].Selected)
{
RadioButtonList1.Items[1].Attributes.Add("style", "background-color: red;");
}
}
答案 0 :(得分:1)
我没有和IDE在我面前,但是如果你想让他们切换为一个是红色而另一个是绿色,请试试这个:
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (RadioButtonList1.Items[0].Selected)
{
RadioButtonList1.Items[0].Attributes.Add("style", "background-color: green;");
RadioButtonList1.Items[1].Attributes.Add("style", "background-color: red;");
}
if (RadioButtonList1.Items[1].Selected)
{
RadioButtonList1.Items[1].Attributes.Add("style", "background-color: green;");
RadioButtonList1.Items[0].Attributes.Add("style", "background-color: red;");
}
}
答案 1 :(得分:1)
将autopostback属性设置为true
<asp:RadioButtonList CssClass="listitemcss" AutoPostBack="true" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged" ID="RadioButtonList1" RepeatDirection="Horizontal" runat="server">
<asp:ListItem >