Click here to Skip to main content
Licence 
First Posted 23 Aug 2004
Views 119,500
Bookmarked 37 times

A simple RadioButtonList in a Web DataGrid Column

By | 23 Aug 2004 | Article
A very simple and practical way, how a single RadioButton acts as a RadioButtonList in a web DataGrid server control column.

Introduction

This article exposes, in a very simple and practical way, how a single RadioButton acts as a RadioButtonList in a web DataGrid server control column. It’s nothing about two thousand lines of code, nor custom control, neither signed assembly. I always wonder why do complicated when we can do simple.

Solution

First, in the ItemCreated DataGrid event, I’ll wire the RadioButton CheckedChanged event:

private void DataGrid1_ItemCreated(object sender, 
   System.Web.UI.WebControls.DataGridItemEventArgs e)
{
   //Find the RadioButton control
   RadioButton oRb = ((RadioButton)e.Item.FindControl("rb"));             
   if(oRb != null)
   {                    
      //Wire the RadioButton's event
      oRb.CheckedChanged += new
         System.EventHandler(this.rb_CheckedChanged);
   }
}

Second, in the RadioButton CheckedChanged event, I’ll loop through DataGrid items to find the previous and current radio buttons and, if found, the previously checked button will be unchecked:

private void rb_CheckedChanged(object sender, System.EventArgs e)
{
   //Find the current selected RadioButton
   RadioButton oRb1 = (RadioButton)sender;

   foreach(DataGridItem oItem in DataGrid1.Items)
   {                
      //Find the previous selected RadioButton
      RadioButton oRb2 = (RadioButton)oItem.FindControl("rb");

      //Display info for the current selected button
      if( oRb2.Equals(oRb1) )
      {                    
         Message.Text = 
            String.Format("Selected Id is: {0}, 
                         and the Description is:{1}",
                         ((Label)oItem.FindControl("lblId")).Text,
                         ((Label)oItem.FindControl("lblDesc")).Text);
      }
       //Uncheck previously selected button
       else 
          oRb2.Checked = false;
   }
}

The info is displayed in a message label after I’ve recuperated the lblId and lblDesc text from the HTML web form, like in the code below:

<asp:label id="Message" Runat="server"></asp:label>
...
<Columns>
   <asp:TemplateColumn HeaderText="Id">
      <ItemTemplate>
         <asp:Label id="lblId" runat="server" Text='<%# 
            DataBinder.Eval(Container.DataItem, "Id") %>' />
      </ItemTemplate>
   </asp:TemplateColumn>

   <asp:TemplateColumn HeaderText="Description">
      <ItemTemplate>
         <asp:Label id="lblDesc" runat="server" Text='<%# 
            DataBinder.Eval(Container.DataItem,"Description") %>' />
      </ItemTemplate>
   </asp:TemplateColumn>
... 
</Columns>
...

The DataGrid is bound to an ArrayList aList in the BindDataGrid():

private void BindDataGrid()
{
   ArrayList aList = new ArrayList();
      
   for(int i=1; i<6; i++)
   {
      aList.Add(new ItemInfo(i, "Desc " + i.ToString()));
   } 

   //Binding and displaying the info in the DataGrid
   DataGrid1.DataSource = aList;
   DataGrid1.DataBind();
}

For more details, please download the source code above. You have nothing else to do than to create a new C# project for an ASP.NET application and add the source code files. That’s all about it.

Enjoy!

License

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

Socrate1

Web Developer

Canada Canada

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalthe same in detail datagrid Pinmemberasily12:22 31 Oct '05  
Generaldon't need to be this complicated... PinsussAnonymous16:04 19 Sep '05  
GeneralDoesn't work as user control Pinmembertheredder17:58 10 Apr '05  
GeneralRe: Doesn't work as user control PinmemberAndrés Ortíz12:13 14 Apr '05  
GeneralRe: Doesn't work as user control Pinmemberx_h1b_y12:57 3 Jul '06  
GeneralWhy things are complicated Pinmemberseby1721570:40 29 Oct '04  
GeneralRe: Why things are complicated Pinmembercojocam8:16 9 Nov '04  
GeneralFor VB.NET Pinmemberhoahong17:21 26 Aug '04  
GeneralRe: For VB.NET Pinmembercojocam3:14 27 Aug '04  
QuestionHow to Convert this Source into Vb.NET Pinmemberhoahong16:44 26 Aug '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web03 | 2.5.120528.1 | Last Updated 24 Aug 2004
Article Copyright 2004 by Socrate1
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid