我无法让单选按钮列表中的单选按钮在 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/