c# - 单选按钮列表未正确选择新值

标签 c# asp.net radiobuttonlist

我无法让单选按钮列表中的单选按钮在 IF 语句中被选中。当 IF 为真时,其他一切都正常工作,除了这个。 Request Pending 是默认值,但我需要能够在 IF 为 True 时选择“Waiting for Approval”按钮。

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

它们似乎都不起作用,我不明白为什么

最佳答案

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

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

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

关于c# - 单选按钮列表未正确选择新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17731032/

相关文章:

c# - 创建辅助 x 轴

asp.net - IIS URL 重写中小写 WWW HTTP 的单一规则

php - Yii2:如何在 Controller 中使用单选按钮值进一步执行?

asp.net - HTTPWebRequest "PUT"错误状态 405 IIS7 中不允许的方法

c# - 我怎样才能防止asp :RadioButtonList from rendering a HTML-Table?

asp.net - 如何在字段集中对齐我的单选按钮列表

c# - Coldfusion - 如何实时更新表格单元格?

c# - 如何告诉 EF 不要在 Code First 中更新引用类型属性?

c# - 私有(private)类中公共(public)属性的可访问性是什么?

javascript - IE 7 对象中的 Angularjs 错误不支持属性或方法 'querySelector'