Solved
Why radiobutton selection on C# server does not work?
Posted on 2011-09-08
I have a group radio buttons defined below. Assume using server side process OnCheckedChanged instead of client side, please see the server function DevSelected.
<asp:Panel ID="pldev" runat="server" GroupingText="Choose a Type" Width="370px">
<asp:RadioButton id="type1" Text="Device1" GroupName="device" AutoPostBack="true" OnCheckedChanged="DevSelected" runat="server" />
<asp:RadioButton id="type2" Text="Device2" GroupName="device" AutoPostBack="true" OnCheckedChanged="DevSelected" runat="server" />
<asp:RadioButton id="type3" Text="Device3" GroupName="device" AutoPostBack="true" OnCheckedChanged="DevSelected" runat="server" />
<asp:RadioButton id="type4" Text="Device4" GroupName="device" AutoPostBack="true" OnCheckedChanged="DevSelected" runat="server" />
</asp:Panel>
protected void DevSelected(object sender, System.EventArgs e)
{
string aclick = (string)Session["TypeSelected"];
if (aclick == "NoneSelected") //Handles the initial click
{
MakeSwitch();
if (type1.Checked) aclick = "Type1Selected";
if (type2.Checked) aclick = "Type2Selected";
if (type3.Checked) aclick = "Type3Selected";
if (type4.Checked) aclick = "Type4Selected";
Session.Add("TypeSelected", aclick);
}
else if (aclick != "NoneSelected") //Handles the clicks after the initial clicking
{
if (MessageBox.Show("would you want to make a switch and ignore these data?", "Confirm Switch", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
MakeSwitch();
}
else
{
//Recover the previos selected button selection if No/Cancel of the confirm box is chosen.
if (aclick == "Type1Selected") type1.Checked = true;
if (aclick == "Type2Selected") type2.Checked = true;
if (aclick == "Type3Selected") type3.Checked = true;
if (aclick == "Type4Selected") type4.Checked = true;
}
}
}
I have the session variable TypeSelected initially set as "NoneSelected" at the application loading. The four radio buttons are unselected initially.
The above code works expected if Yes/Ok of the confirm box is chosen. But if No/Cancel of the confirm box is chosen, the previously selected radio button is not reselected, which means the following code does not work at all. But why? How can I make a radiobutton selection on C# server side?
if (aclick == "Type1Selected") type1.Checked = true;
if (aclick == "Type2Selected") type2.Checked = true;
if (aclick == "Type3Selected") type3.Checked = true;
if (aclick == "Type4Selected") type4.Checked = true;