Detecting which radio button is clicked.

3 posts, 0 answers
  1. Chris
    Chris avatar
    15 posts
    Member since:
    Jun 2011

    Posted 07 Dec 2011 Link to this post

    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. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 08 Dec 2011 Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Chris
    Chris avatar
    15 posts
    Member since:
    Jun 2011

    Posted 09 Dec 2011 Link to this post

    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
Back to Top