1

我无法从单选按钮列表中获取单选按钮以在 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";

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

4

3 回答 3

2

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

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

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

于 2013-07-18T18:19:24.680 回答
2

你可以试试

rbGiftStatus.SelectedIndex = 3; //index max is 3 for 4 elements
于 2013-07-18T18:22:14.930 回答
2

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

尝试 rbGiftStatus.SelectedIndex = 3;

于 2013-07-18T18:30:05.757 回答