【问题标题】:ASP.NET RadioButtonList loosing class="selected" when selection changes选择更改时,ASP.NET RadioButtonList 失去 class="selected"
【发布时间】:2016-05-02 12:33:22
【问题描述】:

我创建了一个非常简单的 RadioButtonList:

<asp:RadioButtonList ID="rblDisplayAs" runat="server" AutoPostBack="true" RepeatLayout="UnorderedList" CssClass="tab-nav">
    <asp:ListItem Selected="True" Text="List View" Value="list"></asp:ListItem>
    <asp:ListItem Text="Calendar View" Value="cal"></asp:ListItem>
</asp:RadioButtonList>

当页面加载时,它呈现如下:

<ul id="pagecontent_rblDisplayAs" class="tab-nav">
    <li class="selected">
        <input id="pagecontent_rblDisplayAs_0" type="radio" checked="checked" value="list">
        <label for="pagecontent_rblDisplayAs_0">List View</label>
    </li>
    <li>
        <input id="pagecontent_rblDisplayAs_1" type="radio" value="cal">
        <label for="pagecontent_rblDisplayAs_0">Calendar View</label>
    </li>
</ul>

我为“选定”类添加了一些 CSS 规则。但是,当用户单击单选按钮时,“已选择”类被删除,并且不会应用于新选择的选项。

如何确保当前选中/选中单选按钮的列表项始终具有“选中”类?

编辑:

如果我将这个添加到后面的代码中:

Protected Sub rblDisplayAs_SelectedIndexChanged(sender as Object, e AS EventArgs) Handles rblDisplayAs.SelectedIndexChanged
    rblDisplayAs.SelectedItem.Attributes.Add("class", "selected")
End Sub

然后在我更改选择后,它呈现为:

<ul id="pagecontent_rblDisplayAs" class="tab-nav">
    <li>
        <input id="pagecontent_rblDisplayAs_0" type="radio" checked="checked" value="list">
        <label for="pagecontent_rblDisplayAs_0">List View</label>
    </li>
    <li>
        <span class="selected">
            <input id="pagecontent_rblDisplayAs_1" type="radio" value="cal">
            <label for="pagecontent_rblDisplayAs_0">Calendar View</label>
        </span>
    </li>
</ul>

编辑:

这是CSS。网站上还有其他地方不是由同样使用这些样式的 asp.net 控件生成的。

ul.tab-nav {
    overflow: hidden; 
    margin: 0px; 
    padding: 0px; 
    position: relative;
    height: 35px;
}
ul.tab-nav li {
    list-style: none; 
    margin: 0px; 
    padding: 0px; 
    display: inline; 
    position: relative; 
    top: 5px; 
    line-height: 1.6;
    background: none;
}
ul.tab-nav li.selected {
    top: 0px;
    z-index: 10;
}
ul.tab-nav li input {
    display:none;
}
ul.tab-nav li a, ul.tab-nav li label
    font-size: 116.67%;
    font-size: 1.1667rem;
    color: #a6a6a6;
    font-family: 'FrutigerRoman', Arial;
    text-decoration: none;
    border: 1px solid #ccc;
    border-bottom: 0px;
    padding: 2px 2px 0px 2px;
    display: inline-block;
    position: relative;
    height: 26px;
    border-radius: 2px 2px 0px 0px;
    background: #e9e9e9;
}
ul.tab-nav li.selected a, ul.tab-nav li.selected label {
    border-bottom: 1px solid #fff; 
    padding: 2px 2px 5px 2px; 
    bottom: 0px; 
    color: #00619d;
    background: #fff;
}

【问题讨论】:

  • 您能否为“选定”课程发布您的 CSS?
  • 我已将 css 添加到最初的问题中。
  • 我想知道我是否只需要对 jQuery 和 OnClientClick 做一些事情......或者,单选按钮没有 OnClientClick 但类似......

标签: asp.net


【解决方案1】:

感谢您将 ul 的 css 添加到您的问题中。

您的代码中的最后一个 css 类需要稍作更改才能使所选样式正常工作。这是在测试网络应用程序中为我工作的完整的 aspx、代码和 css。

ASPX

<asp:RadioButtonList ID="rblDisplayAs" runat="server" AutoPostBack="true" RepeatLayout="UnorderedList" OnSelectedIndexChanged="rblDisplayAs_SelectedIndexChanged" CssClass="tab-nav">
    <asp:ListItem Selected="True" Text="List View" Value="list"></asp:ListItem>
    <asp:ListItem Text="Calendar View" Value="cal"></asp:ListItem>
</asp:RadioButtonList>

代码背后

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack) return;
    foreach (var li in rblDisplayAs.Items.Cast<ListItem>().Where(li => li.Selected))
        li.Attributes.Add("class", "selected");
}

protected void rblDisplayAs_SelectedIndexChanged(object sender, EventArgs e)
{
    rblDisplayAs.SelectedItem.Attributes.Add("class", "selected");
}

CSS

ul.tab-nav {
    overflow: hidden; 
    margin: 0; 
    padding: 0; 
    position: relative;
    height: 35px;
}

ul.tab-nav li {
    list-style: none; 
    margin: 0; 
    padding: 0; 
    display: inline; 
    position: relative; 
    top: 5px; 
    line-height: 1.6;
    background: none;
}

ul.tab-nav li.selected {
    z-index: 10;
}

ul.tab-nav li input {
    display:none;
}

ul.tab-nav li a, ul.tab-nav li label {
    font-size: 116.67%;
    font-size: 1.1667rem;
    color: #a6a6a6;
    font-family: 'FrutigerRoman', Arial;
    text-decoration: none;
    border: 1px solid #ccc;
    border-bottom: 0;
    padding: 2px 2px 0 2px;
    display: inline-block;
    position: relative;
    height: 26px;
    border-radius: 2px 2px 0 0;
    background: #e9e9e9;
}

ul.tab-nav .selected label {
    border-bottom: 1px solid #fff; 
    padding: 2px 2px 5px 2px; 
    bottom: 0;
    top: -5px;
    color: #00619d;
    background: #fff;
}

【讨论】:

  • 不过,我需要工作的不仅仅是标签的颜色; li 本身在选择时具有不同的样式(top:0 与 top:5px)。
  • 我在上面的答案中修改了css。这能满足您的需要吗?
  • 看起来应该可以了。我实际上实现了一个 javascript 解决方案,以便在收音机选择更改时将类应用到正确的 li ......当我回到工作岗位时,我会尽量记住将它作为替代解决方案发布。
猜你喜欢
  • 1970-01-01
  • 2013-06-17
  • 1970-01-01
  • 2012-05-13
  • 2014-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多