【问题标题】:Radio Button List not properly selecting new value单选按钮列表未正确选择新值
【发布时间】:2013-07-19 02:30:05
【问题描述】:

我无法从单选按钮列表中获取单选按钮以在 IF 语句中被选中。当 IF 为真时,其他一切都正常工作,除了这个。 Request Pending 是默认值,但我需要能够在 IF 为 True 时选择“等待批准”按钮。

我的 HTML 代码是:

<asp:RadioButtonList ID="rbGiftStatus" RepeatDirection="Horizontal" 
    runat="server" TabIndex="3">
    <asp:ListItem Text="Request Pending" Selected="True" Value="1"></asp:ListItem>
    <asp:ListItem Text="Check Pending" Value="2"></asp:ListItem>
    <asp:ListItem Text="Completed" Value="3"></asp:ListItem>
    <asp:ListItem Text="Waiting for Approval" Value="4"></asp:ListItem>
</asp:RadioButtonList>

我的 C# 是这样的:

rbGiftStatus.SelectedIndex = 4;

我也尝试过其他方法,例如:

rbGiftStatus.Text = "4";
rbGiftStatus.SelectedItem = "4";
rbGiftStatus.SelectedValue = "4";

它们似乎都不起作用,我不知道为什么

【问题讨论】:

    标签: c# asp.net radiobuttonlist


    【解决方案1】:

    SelectedIndex 是正确的方式:Set Radiobuttonlist Selected from Codebehind

    但是,您使用的 SelectedIndex 为 4,超出了数组的范围。 C# 是基于 0 的索引,因此第一项是索引 0。这将使您的第 4 项索引为 3。

    rbGiftStatus.SelectedIndex = 3; 应该这样做。

    【讨论】:

    • 当代码编译时,这仍然没有改变单选按钮列表的选择。我不确定问题是什么,因为几个 lbltest.Visable = true;标签、文本框和其他控件正常运行,只是没有选择 RBL 中正确的单选按钮(澄清一下,没有选择新的单选按钮,默认只是保持选中状态)
    • 从您的第一个项目的 ASPX 标记中取出 Selected="true"。这可能会覆盖您的设置。
    • 试过了,还是不行。我猜代码中还有其他可能会影响这一点的东西。 AJAX 或 Jquery,也许......除此之外,我不知道......我很感激纠正我的索引
    • 这很奇怪。您是否在其他任何地方设置了 SelectedIndex,例如未包含在 if(!IsPostBack) 条件中的 Page_Load
    【解决方案2】:

    你可以试试

    rbGiftStatus.SelectedIndex = 3; //index max is 3 for 4 elements
    

    【讨论】:

      【解决方案3】:

      索引以 0 而不是 1 开头。因此,如果您的 RBL 中有 4 个项目,那么索引范围将为 0-3。在这种情况下,您调用的是不存在的索引 4。

      试试rbGiftStatus.SelectedIndex = 3;

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-17
        • 2016-06-17
        • 2017-08-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多