Search code examples
asp.netradio-buttonshow-hidepanels

Show and hide panel using radio buttons


I have two radio buttons and two panels on my webpage and I would like to show panel 1 when radio button1 checked changed and hide panel2 and similarly panel2 needs to be shown when radio button2 checked changed and hide panel1.

This is how I have done with no success

Here are my radio buttons:

<asp:RadioButton ID="RadioButton1" runat="server" Text="Text" ForeColor="White" GroupName="selection" OnCheckedChanged="RadioButton1_CheckedChanged"/>

<asp:RadioButton ID="RadioButton2" runat="server" Text="Image" ForeColor="White" GroupName="selection" OnCheckedChanged="RadioButton2_CheckedChanged"/>

This is RadioButton1_CheckedChanged:

If RadioButton1.Checked = True Then
            RadioButton2.Checked = False
            Panel1.Visible = True
            Panel2.Visible = False
 End If

This is RadioButton2_CheckedChanged:

 If RadioButton2.Checked = True Then
            RadioButton1.Checked = False
            Panel1.Visible = False
            Panel2.Visible = True
 End If

Solution

  • You need to set autopostback to true for the radio buttons. AutoPostback="True" include that in your radiobutton tag and you should be good to go. A simple debug of your code would show you that both events are not called with your current code.