Solved

Why radiobutton selection on C# server does not work?

Posted on 2011-09-08
4
425 Views
Last Modified: 2012-05-12
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;
0
Comment
Question by:minglelinch
  • 2
4 Comments
 
LVL 4

Expert Comment

by:asp_net2
Comment Utility
Can you please be more specific on what you are trying to accomplish?
0
 
LVL 1

Author Comment

by:minglelinch
Comment Utility
There are only four radio buttons initially on a page with none of them selected.
When a user selects one of the four buttons, the corresponding panels/divs/fields will be displayed below the radiobutton group. The MakeSwitch server function will do this work.

private void MakeSwitch()
    {
        if (type1.Checked == true)
        {
            type1panel.Attributes.Add("style", "display:inline");
            type1fields.Attributes.Add("style", "display:inline");
            ... ...
            type4fields.Attributes.Add("style", "display:none");
            type4buttons.Attributes.Add("style", "display:none");
        }
        if (type2.Checked == true)
        {
            type2panel.Attributes.Add("style", "display:inline");
            type2fields.Attributes.Add("style", "display:inline");
            ... ...
            type4fields.Attributes.Add("style", "display:none");
            type4buttons.Attributes.Add("style", "display:none");
            LoadType2SoemCombList(ref ddltylist);
        }
        ... ...    
    }

When the user click a different radio button, the previous panels would be hidden and new panels would show. This is why  a confirm box pops up.

If the user choose OK of the confirm box, the corresponding panels/fields of the selected button will be displayed.

If the user choose Cancel of the confirm box, the page would keep same with the selected radio button prior to the clicking being recovered to be selected. Because when the code execution goes into the DevSelected function, the newly clicked button has already been selected, and this is why a session variable is used to store the selected button prior to the clicking. Since the previous selected button is memorized, once Cancel of the confirm box is chosen, the button selection is resotred back by the code if (aclick == "Type1Selected") type1.Checked = true; ... ...

Now it seems the following code does not work. The page/fields keep same as expected except the radio button is not resotred. why? Thanks.

                if (aclick == "Type1Selected") type1.Checked = true;
                if (aclick == "Type2Selected") type2.Checked = true;
                if (aclick == "Type3Selected") type3.Checked = true;
                if (aclick == "Type4Selected") type4.Checked = true;
0
 
LVL 21

Accepted Solution

by:
Alfred1 earned 500 total points
Comment Utility
Try the following.  I modified your code at the point of recovering your previous selected button.  I believe your problem is due to ambiguity in the true or false selection.  There can only be one true at any point in time with a group of radiobuttons.  
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 previous selected button selection if No/Cancel of the confirm box is chosen.
                if (aclick == "Type1Selected") { type1.Checked = true; type2.Checked = false; type3.Checked = false; type4.Checked = false; }
                if (aclick == "Type2Selected") { type1.Checked = false; type2.Checked = true; type3.Checked = false; type4.Checked = false; }
                if (aclick == "Type3Selected") { type1.Checked = false; type2.Checked = false; type3.Checked = true; type4.Checked = false; }
                if (aclick == "Type4Selected") { type1.Checked = false; type2.Checked = false; type3.Checked = false; type4.Checked = true; }
            }
        }
    }

Open in new window

0
 
LVL 1

Author Closing Comment

by:minglelinch
Comment Utility
Thanks. Great answer on the server side.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now