Search code examples
c#asp.netradiobuttonlist

Radio Button List not properly selecting new value


I am having trouble getting a radio button from a Radio Button List to become selected in an IF statement. Everything else is working correctly when the IF is true, except for this. Request Pending is the default value, but I need to be able to have the "Waiting for Approval" Button selected when the IF is True.

My HTML code is:

<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>

My C# is this:

rbGiftStatus.SelectedIndex = 4;

and I have tried other ways suchs as:

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

None of them seem to work, and I can't figure out why


Solution

  • SelectedIndex is the correct way: Set Radiobuttonlist Selected from Codebehind

    But, you are using SelectedIndex of 4, which is out of range of the array. C# is 0-based indexing, so the first item is index 0. That would make your 4th item index 3.

    rbGiftStatus.SelectedIndex = 3; should do it.