1

I've got this code to prevent two opposite radio buttoms from both being checked:

RadioButton rbUSCitizenOrPermResY = null;
RadioButton rbUSCitizenOrPermResN = null;

. . .

rbUSCitizenOrPermResY = new RadioButton
{
    CssClass = "finaff-webform-field-input"
}; // doesn't allow assignment to CheckedChanged above
rbUSCitizenOrPermResY.CheckedChanged += new EventHandler(rbUSCitizenOrPermResY_Changed);

. . .

private void rbUSCitizenOrPermResY_Changed(object sender, EventArgs e)
{ 
    if (rbUSCitizenOrPermResN.Checked)
    {
        rbUSCitizenOrPermResN.Checked = false;
    }
}

So, if the "Yes" radio button is checked, it unchecks the "No" radio button if the "No" radio button is checked.

Supposedly.

In actuality, this event handler is entered, but not when the radio button is clicked. Rather, it fires later, after I click the "Save" button. Is this an asynchronous event handler, that just wakes up whenever it feels like it? If so, how can I get it to "straighten up and fly right"/"shape up or ship out"?

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • Can you go at this by forcing them to be mutually exclusive by placing them within the same GroupBox or Panel? – DWright May 05 '15 at 23:27
  • I'm not aware of such a construct in html (this code creates html "behind the scenes" - does it exist? – B. Clay Shannon-B. Crow Raven May 06 '15 at 14:19
  • 1
    So, I'm thinking that what is going on is that only when you click Save is the data posted to the server, so only then is the CheckChanged event processed. Here is the first sentence: "Occurs when the value of the Checked property changes between posts to the server." of the description of the CheckChanged event at https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkbox.checkedchanged%28v=vs.110%29.aspx. So if nothing posts to the server when you just click on a Radiobutton, nothing happens. But when you click save, the server checks the state. Does that make sense? – DWright May 06 '15 at 22:06
  • Yes, makes sense, but confusing for the user to see both radio buttons "checked" – B. Clay Shannon-B. Crow Raven May 06 '15 at 22:27

1 Answers1

1

It's taken me a while to figure out what you are describing, because I'm used to thinking about this stuff client side, not server side. But since you are doing server side, I think you need to introduce some kind of grouping construct for your radio buttons. I just tried an example app where I used a RadioButtonList containing ListItems (rather unintuitively, they are not called Radiobuttons). But using the RadioButtonList to group them, they behave correctly on the client side, i.e. they are exclusive. Clicking on one unselects the other. Here is the markup I used (does this fit your scenario?):

<asp:RadioButtonList ID="RadioButtonList1" runat="server">
    <asp:listItem ID="rad1" runat="server"></asp:listItem>
    <asp:listItem ID="rad2" runat="server"></asp:listItem>
</asp:RadioButtonList>

The listitem control got translated in the designer file as a System.Web.UI.WebControls.ListItem

Incidentally, I mocked this up as a Web Form application in Visual Studio. I don't have any Sharepoint stuff installed, but I think the principle is the same (I hope).

The generated HTML ended up looking like this:

<table id="MainContent_RadioButtonList1">
    <tr>
        <td><span ID="rad1"><input id="MainContent_RadioButtonList1_0" type="radio" name="ctl00$MainContent$RadioButtonList1" value="" /></span></td>
    </tr>
    <tr>
         <td><span ID="rad2"><input id="MainContent_RadioButtonList1_1" type="radio" name="ctl00$MainContent$RadioButtonList1" value="" /></span></td>
    </tr>
</table>
DWright
  • 9,258
  • 4
  • 36
  • 53
  • 1
    I just added the generated HTML so you can see that the controls have type `radio` on the client side, even though they have type listItem on the server side. – DWright May 06 '15 at 22:44