Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Why radiobutton selection on C# server does not work?

Posted on 2011-09-08
4
Medium Priority
?
450 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:
Alfred A. earned 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

877 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