Ryan
Ryan

Reputation: 23

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

Upvotes: 1

Views: 2340

Answers (3)

Bagzli
Bagzli

Reputation: 6569

The index starts with a 0 and not a 1. So if you have 4 items in your RBL then the index range will be 0-3. In this case you are calling an index of 4 which does not exists.

Try rbGiftStatus.SelectedIndex = 3;

Upvotes: 2

Aghilas Yakoub
Aghilas Yakoub

Reputation: 28970

You can try with

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

Upvotes: 2

Garrison Neely
Garrison Neely

Reputation: 3289

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.

Upvotes: 2

Related Questions