Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Why radiobutton selection on C# server does not work?

Posted on 2011-09-08
4
430 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!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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