Solved

Why radiobutton selection on C# server does not work?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

914 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

20 Experts available now in Live!

Get 1:1 Help Now