This is a migrated thread and some comments may be shown as answers.

Detecting which radio button is clicked.

2 Answers 467 Views
Button
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 07 Dec 2011, 04:04 PM
Hi,
I have a Radio button group on my page as part of a form . How do i detect which of the radio buttons has been selected in the server side code.? Is there an easier way than testing each button to see if it is checked?

Chris

 

<telerik:RadButton ID="NordicInclBtn" runat="server" GroupName="NordicsGroup" Checked="true"
  
Skin="Office2010Blue" Text="RadButton" ToggleType="Radio" AutoPostBack="false">
  
<ToggleStates>
  
<telerik:RadButtonToggleState Text="Include" PrimaryIconCssClass="rbToggleRadioChecked" />
  
<telerik:RadButtonToggleState Text="Include" PrimaryIconCssClass="rbToggleRadio" />
  
</ToggleStates>
  
</telerik:RadButton>
  
<telerik:RadButton ID="NordicExcBtn" runat="server" GroupName="NordicsGroup" AutoPostBack="false"
  
Skin="Office2010Blue" Text="RadButton" ToggleType="Radio">
  
<ToggleStates>
  
<telerik:RadButtonToggleState Text="Exclude" PrimaryIconCssClass="rbToggleRadioChecked" />
  
<telerik:RadButtonToggleState Text="Exclude" PrimaryIconCssClass="rbToggleRadio" />
  
</ToggleStates>
  
</telerik:RadButton>
  
<telerik:RadButton ID="NordicOnlyBtn" runat="server" GroupName="NordicsGroup" AutoPostBack="false"
  
Skin="Office2010Blue" Text="RadButton" ToggleType="Radio">
  
<ToggleStates>
  
<telerik:RadButtonToggleState Text="Only" PrimaryIconCssClass="rbToggleRadioChecked" />
  
<telerik:RadButtonToggleState Text="Only" PrimaryIconCssClass="rbToggleRadio" />
  
</ToggleStates>
  
</telerik:RadButton>

 

 

 

 

2 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 08 Dec 2011, 07:37 PM
Hello Chris, 

if you do not want to use loop then take one hiddenfield and when you checked any radio button at that time you can store their value/id in this hiddenfield and in server side using hiden field you can get the information about selected button.

OR

List<RadButton> lbtn = (from r in this.form1.Controls.OfType<RadButton>() // (from r in this.Controls.OfType<RadButton>()
                                    where ((RadButton)(r)).Checked == true
                                     select r).ToList();
 
 
            if (lbtn.Count > 0)
            {
                Response.Write(lbtn[0].Text);
            }



Thanks,
Jayesh Goyani
0
Chris
Top achievements
Rank 1
answered on 09 Dec 2011, 03:37 PM
HI,
I openned a support ticket and they suggested a slightly different approach whic i have adjusted to suit my needs.
I placed each button group in a placeholder and gave each button a command name. Now i can have multiple toggle button groups and when the form submit occours i just call the following method passing the placeholder and in return i get the CommandName of the checked button  -:
protected String GetCheckedCommand(PlaceHolder holder)
        {
            foreach (Control btn in holder.Controls)
            {
                RadButton rb = (RadButton)btn;
  
                if (rb.Checked)
                {
                    return rb.CommandName;
                }
            }
  
            return null;
        }


Hope tihis helps people.

Chris
Tags
Button
Asked by
Chris
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Chris
Top achievements
Rank 1
Share this question
or