Solved

Why radiobutton selection on C# server does not work?

Posted on 2011-09-08
4
429 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
ID: 36503115
Can you please be more specific on what you are trying to accomplish?
0
 
LVL 1

Author Comment

by:minglelinch
ID: 36503719
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
ID: 36504275
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
ID: 36505668
Thanks. Great answer on the server side.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Regular expression help 2 25
List<PaisEntity> - show some 1 24
RLDC Reporting in Visual studio 11 16
MVC DDL Json Not Binding to Model 2 13
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

777 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