C# 在Asp net c中更改单选按钮列表中的列表项颜色#
我正在使用asp net中的radio button列表创建两个单选按钮,当选中该按钮时,应更改颜色,第一个为绿色,第二个为红色,但我无法更改所述颜色C# 在Asp net c中更改单选按钮列表中的列表项颜色#,c#,asp.net,visual-studio,visual-studio-2017,C#,Asp.net,Visual Studio,Visual Studio 2017,我正在使用asp net中的radio button列表创建两个单选按钮,当选中该按钮时,应更改颜色,第一个为绿色,第二个为红色,但我无法更改所述颜色 Tldr:更改为列表项1,列表项内的小圆圈变为绿色,更改为列表项2,小圆圈变为红色。 此外,由于某些原因,它无法在radiobuttonlist1.Items中识别我的radiobuttonlist1 这是我的html代码 <asp:RadioButtonList CssClass="listitemcss" OnSelectedIndex
Tldr:更改为列表项1,列表项内的小圆圈变为绿色,更改为列表项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>
我前面没有和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;");
}
}
将autopostback属性设置为true
<asp:RadioButtonList CssClass="listitemcss" AutoPostBack="true" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged" ID="RadioButtonList1" RepeatDirection="Horizontal" runat="server">
<asp:ListItem >
setAutoPostBack
Trueit表示当前版本中不存在RadioButtonList1context@Rasec219你的原始代码没有出现这个错误?
<asp:RadioButtonList CssClass="listitemcss" AutoPostBack="true" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged" ID="RadioButtonList1" RepeatDirection="Horizontal" runat="server">
<asp:ListItem >