Firing OnCheckedChanged event with ASP:RadioButton

DevDave picture DevDave · May 2, 2012 · Viewed 10.8k times · Source

I have the following RadioButtons:

<asp:RadioButton id="rdoStandard" runat="server" Checked="true" GroupName="delivery" OnCheckedChanged="Check_Clicked" />

<asp:RadioButton ID="rdoExpress" runat="server" GroupName="delivery" OnCheckedChanged="Check_Clicked" />

<asp:RadioButton ID="rdoNextDay" runat="server" GroupName="delivery" OnCheckedChanged="Check_Clicked" />

And in the code behind I have this:

 Protected Sub Check_Clicked(ByVal sender As Object, ByVal e As EventArgs) Handles rdoStandard.CheckedChanged, rdoExpress.CheckedChanged, rdoNextDay.CheckedChanged
            RaiseEvent DeliveryOptionChanged()
        End Sub

I have tried the above but also tried different ways, such as a click method for each of the RadioButtons and I never hit the Check_Clicked method.

I do not want to AutoPostBack, and cannot see why you would want to if you were firing an event?

Its been a while since I used VB and WebForms, Can someone help me with this?

Answer

Tim Schmelter picture Tim Schmelter · May 2, 2012

You need to set AutoPostBack to true if you want to trigger the event immediately, default is false.

<asp:RadioButton id="rdoStandard" AutoPostback="true" runat="server" Checked="true" GroupName="delivery" OnCheckedChanged="Check_Clicked" />

Note that a RadioButton inherits from CheckBox, therefor it inherits the AutoPostback property therefrom

Edit: If you want to avoid a postback, this event is also raised on the next roundtrip to the server when you don't set AutoPostback to true. You could for example use a submit-button, both, the Button's and also the CheckBox' click events are raised successive.