Click here to Skip to main content
13,555,707 members
Click here to Skip to main content
Add your own
alternative version


29 bookmarked
Posted 8 Oct 2004

More on Grouping ASP.NET RadioButtons

, 8 Oct 2004
Rate this:
Please Sign up or sign in to vote.
Grouping ASP.NET RadioButtons.


There is one deficiency in ASP.NET RadioButton Server control. When it is nested to a DataGrid template, the resultant multiple radio buttons will not work as a group. Vladimir Smirnov has remedied this in his article: How to group RadioButtons. Recently, when I wanted to use his control, it is found that it does not support autopostback event very well. I would like it to behave similar to a command button. After a little bit effort, I made it work, and the following is the changed part. In addition, I also want to share some other methods to achieve the same effect.


When user clicks any radio button, the event will be routed to the RadioButton class. However, what is missing here is it does not bubble this event to its parent. We need to override onBubbleEvent function. First of all, we add two properties as follows:

private string commandName;
public string CommandName 
  get{return this.commandName;}
  set{this.commandName = value;}

public string CommandArgument
    object o = this.ViewState["CommandArgument"];
    if (o == null)
      return null;
      return System.Convert.ToString(o);
  set{this.ViewState["CommandArgument"] = value;}

CommandArgument is better to be of type object. Here, I simply declare it to be string. CommandName will not be saved across page postback. If you want to use that way, change it as in CommandArgument.

The following code is onBubbleEvent. It bridges RadioButton and its parent because it will call RaiseBubbleEvent which will finally invoke event on its parent. Here, only DataGrid is considered. If you want this RadioButton to work within DataList and Repeater, you can add similar code:

protected override bool OnBubbleEvent(object source, EventArgs args)
  if ((this.NamingContainer as DataGridItem ) != null)
    CommandEventArgs cmdarg = new CommandEventArgs(this.commandName, 
    DataGridCommandEventArgs e = new 
               source, cmdarg);
    return true;
  return false;

void IPostBackDataHandler.RaisePostDataChangedEvent()

Now, it is done! When you use it, specify CommandName and CommandArgument if necessary and respond to event in DataGrid's ItemCommand event handler, the same way as button.

Small Tips

Autopostback event will happen even if it is already selected. I found that ASP.NET RadioButtonList's autopostback event works also like this. Actually, it is not necessary. So, I made some small changes to correct this. In RenderInputTag method, add the following code:

//onClick += Page.GetPostBackClientEvent(this, String.Empty);
onClick += "if(this.selected != null) return true; " + 
         Page.GetPostBackClientEvent(this, String.Empty);

Here, when a radio button is checked, it will generate an extra attribute selected="X". So for checked radio button, the if condition will evaluate to true and then return immediately such that postback event will not be invoked.

Three other ways to work around

  1. In Page_Load function, find radio button control in DataGrid item and add event handler.
  2. Use non-display button plus client-side radio button. It needs to bind button ClientID to radio button's onclick event.
  3. Use non-display button plus image mimicking radio button.

For, non-display button and databinding technique, you can reference: Enable DataList Row Highlighting and Click Event PostBack.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralSample code for modifying the GroupRadioButton Pin
joshigaurava28-Jun-06 1:34
memberjoshigaurava28-Jun-06 1:34 
GeneralGroup radiobuttons AutopostBack Pin
Alex vn17-Apr-06 21:01
memberAlex vn17-Apr-06 21:01 
GeneralSledgehammer Pin
metamind1-Feb-05 4:40
sussmetamind1-Feb-05 4:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04-2016 | 2.8.180515.1 | Last Updated 8 Oct 2004
Article Copyright 2004 by billxie
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid