在ASP.NET C#中更改单选按钮列表中的列表项颜色

我正在使用ASP.NET的无线电RadioButtonList的创建2单选按钮选择时都应该改变颜色第一个是绿色的,而第二个红色,但我不能够做出改变表示颜色

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>  

这是我的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;");        

    }

}


LEATH
浏览 307回答 2
2回答

慕勒3428872

我前面没有IDE,但是如果您希望它们切换为红色为红色而另一个为绿色,请尝试以下操作:protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e){&nbsp; &nbsp; &nbsp; &nbsp; if (RadioButtonList1.Items[0].Selected)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RadioButtonList1.Items[0].Attributes.Add("style", "background-color: green;");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RadioButtonList1.Items[1].Attributes.Add("style", "background-color: red;");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (RadioButtonList1.Items[1].Selected)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RadioButtonList1.Items[1].Attributes.Add("style", "background-color: green;");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RadioButtonList1.Items[0].Attributes.Add("style", "background-color: red;");&nbsp; &nbsp; &nbsp; &nbsp; }}

www说

将autopostback属性设置为true&nbsp;<asp:RadioButtonList CssClass="listitemcss" AutoPostBack="true" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged" ID="RadioButtonList1" RepeatDirection="Horizontal" runat="server">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <asp:ListItem >
打开App,查看更多内容
随时随地看视频慕课网APP