C# – RadioButtonList SelectedItem always returns EmptyString

asp.netcradiobuttonlist

I have this custom radiobuttonlist I'm using in my .aspx page in order to be able to get the GroupName to actually work since I will have 2 RadiobuttonList controls on the same .aspx page:

public class CustRadioButtonList : RadioButtonList, IRepeatInfoUser
{
    void IRepeatInfoUser.RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
    {
        RadioButton radioButton = new RadioButton();
        radioButton.Page = this.Page;
        radioButton.GroupName = "radioButtonGroup";
        radioButton.ID = this.ClientID + "_" + repeatIndex.ToString();
        radioButton.Text = this.Items[repeatIndex].Text;
        radioButton.Attributes["value"] = this.Items[repeatIndex].Value;
        radioButton.Checked = this.Items[repeatIndex].Selected;
        radioButton.TextAlign = this.TextAlign;
        radioButton.AutoPostBack = this.AutoPostBack;
        radioButton.TabIndex = this.TabIndex;
        radioButton.Enabled = this.Enabled;

        radioButton.RenderControl(writer);
    }
}

So that's just a simple extentension where I set the GroupName to be sure that all the radiobuttons created by that RadioButtonList have the same GroupName so that now, if anyone selects a value from RadiobuttonList1, it deselects any value they have selected in Radiobutton2 and vice versa (so they are mutually exclusive sets of radiobuttons).

Note: The radiobuttonlists are definitely being binded via a method call that's wrapped in a check for !Page.IsPostBack so that is not the issue here.

Here is an example where I'm using it in my .aspx page:

 <aj:CustRadioButtonList checked="false"  ID="rblEmail" runat="server" />

 <aj:CustRadioButtonList checked="false"  ID="rblReason" runat="server" />

Here in my code-behind, I'm checking for the selectedValue from the rblEmail within an onclick even of a button on my page..but it always returns empty string even if I have selected an item in the list:

    protected void btnContinue_Click(object sender, EventArgs e)
    {
        _actionID = rblEmail.SelectedValue;

I've spent an entire day around trying to figure out now why I keep getting an emptystring when clearly I have selected a value in rblEmail. Same holds true for the other radiobuttonlist rblReason. In either case, when checking from code-behind I get emptystring for SelectedValue.

If you look at the markup, here's how it looks:

<table id="rblEmail" checked="false" border="0">
    <tr>
        <td><input id="rblEmail_0" type="radio" name="radioButtonGroup" value="0" /><label for="rblEmai_0">All Offers</label></td>

    </tr><tr>
        <td><input id="rblEmail_1" type="radio" name="radioButtonGroup" value="1" /><label for="rblEmail_1">week</label></td>
    </tr><tr>
        <td><input id="rblEmail_2" type="radio" name="radioButtonGroup" value="2" /><label for="rblEmail_2">month</label></td>
    </tr><tr>
        <td><input id="rblEmail_3" type="radio" name="radioButtonGroup" value="3" /><label for="rblEmail_3">Holiday</label></td>
    </tr>

</table>
            </div>
...

<table id="rblReason" checked="false" border="0">
    <tr>
        <td><input id="rblReason_0" type="radio" name="radioButtonGroup" value="1" /><label for="rblReason_0">I receive</label></td>
    </tr><tr>
        <td><input id="rblReason_1" type="radio" name="radioButtonGroup" value="2" /><label for="rblReason_1">I have no need</label></td>
    </tr><tr>
        <td><input id="rblReason_2" type="radio" name="radioButtonGroup" value="3" /><label for="rblReason_2">Other</label></td>

    </tr>
</table> 

Best Answer

Don't know exactly why this isn't working, but it seems like a little javascript would provide an easy workaround. Instead of a custom control, just use regular RadioButton objects... then attach some javascript to clear the selection on List1 when something on List2 is selected, and vice versa.

Not sure why you'd use any sort of repeater here if you already know how many radiobuttons there would be? (per your comments above)

Related Topic