【问题标题】:Change list item color inside radio button list in Asp net c#在 Asp net c# 中更改单选按钮列表内的列表项颜色
【发布时间】:2018-09-26 14:25:50
【问题描述】:

I am using a radio RadioButtonList in asp net to create 2 radio button that when selected are supposed to change color the first one being green and the second one red but i am not able to make the change said color
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;");        
    }
}

【问题讨论】:

    标签: c# asp.net visual-studio visual-studio-2017


    【解决方案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;");
    
            }
    
    
    }
    

    【讨论】:

    • 它说 RadioButtonList1 在当前上下文中不存在
    • @Rasec219 你的原始代码没有出现这个错误?
    【解决方案2】:

    将自动回发属性设置为 true

     <asp:RadioButtonList CssClass="listitemcss" AutoPostBack="true" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged" ID="RadioButtonList1" RepeatDirection="Horizontal" runat="server">
                                        <asp:ListItem >
    

    【讨论】:

    • 设置AutoPostBack真
    猜你喜欢
    • 1970-01-01
    • 2023-04-01
    • 2016-07-19
    • 2014-08-27
    • 1970-01-01
    • 2021-06-19
    • 2010-12-23
    • 2011-04-08
    • 1970-01-01
    相关资源
    最近更新 更多