Click here to Skip to main content
15,908,020 members
Articles / Web Development / ASP.NET
Article

Adding and Using RadioButton to DataGrid in ASP.NET

Rate me:
Please Sign up or sign in to vote.
1.75/5 (24 votes)
9 May 2004 152.6K   32   13
Adding and using RadioButton to DataGrid in ASP.NET.

Introduction

How can we add RadioButtons to a DataGrid in ASP.NET? And how can we use server side scripts when we click a RadioButton on the grid? This is the (smart :) solution.

Using the code

Use this script to create RadioButtons. When a RadioButton is clicked, OnCheckedChanged event will be fired. Write a server-side code to catch this event.

ASP.NET
<asp:DataGrid id="dgOrnek" runat="server" 
 AutoGenerateColumns="False">
  
   <Columns>
     <asp:TemplateColumn>
       <ItemTemplate>
         <asp:RadioButton AutoPostBack=True 
             OnCheckedChanged="DetayGoster" 
             id="rbsira" Text='deneme' runat="server"/>
       <ItemTemplate>
     <TemplateColumn>
   <Columns>
C#
string sRbText="";
 public void DetayGoster(object sender,EventArgs e) {
     RadioButton rb = new RadioButton();
     rb = (RadioButton) sender;
     sRbText = rb.ClientID;
 
     foreach (DataGridItem i in dgOrnek.Items) 
     {
         rb    = (RadioButton) i.FindControl ("rbsira");
         rb.Checked = false;
         if (sRbText==rb.ClientID)
         {
             rb.Checked = true;
             txtSiraNo.Text = rb.Text.Trim(); 
  // if you want to get a property of the selected id
         }
     }
  }

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


Written By
Web Developer
Turkey Turkey
I am from Turkey and working on MRP/ERP Projects.

Comments and Discussions

 
QuestionHow to group radio buttons / inputs inside ASP.NET ListView and GridView Pin
Evaldas Jocys10-Oct-08 1:23
Evaldas Jocys10-Oct-08 1:23 
Generalhaj Pin
haja me28-Sep-07 23:44
haja me28-Sep-07 23:44 
Generalagain. Pin
ibizq9-Oct-06 20:30
ibizq9-Oct-06 20:30 
Generalradiobutton to datagrid (Using javascrip) Pin
ibizq9-Oct-06 20:28
ibizq9-Oct-06 20:28 
Questionhow about Pin
{07E21C0D-C1B0-4236-BA41-6D1C73429BE4}6-Aug-06 15:02
{07E21C0D-C1B0-4236-BA41-6D1C73429BE4}6-Aug-06 15:02 
GeneralAnother approach Pin
JVMFX3-Feb-06 9:57
JVMFX3-Feb-06 9:57 
GeneralRe: Another approach Pin
pdangelo13-Feb-06 6:04
pdangelo13-Feb-06 6:04 
GeneralRe: Another approach Pin
JVMFX13-Feb-06 7:00
JVMFX13-Feb-06 7:00 
GeneralRe: Another approach Pin
pdangelo13-Feb-06 19:28
pdangelo13-Feb-06 19:28 
GeneralConvert into VB.net! Pin
Do Hong Nhung3-Feb-05 22:05
Do Hong Nhung3-Feb-05 22:05 
GeneralRe: Convert into VB.net! Pin
habaneroQueen27-Feb-05 5:52
habaneroQueen27-Feb-05 5:52 
GeneralRe: Convert into VB.net! Pin
Specificity22-Jun-05 7:46
sussSpecificity22-Jun-05 7:46 
insert into your datagrid:

<asp:TemplateColumn>
<ItemTemplate>
<asp:RadioButton id="rbSelection" runat="server" OnCheckedChanged="rbChanged"
AutoPostBack="true">
</asp:RadioButton>
</ItemTemplate>
</asp:TemplateColumn>


insert into your code behind page:

Public Sub rbChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Label1.Text = sender.ClientID
Dim i As DataGridItem
Dim rb As RadioButton
For Each i In dtgNew.Items
rb = i.FindControl("rbSelection")
If Not rb.ClientID = sender.ClientID Then
rb.Checked = False
Else
dtgNew.SelectedIndex = i.ItemIndex
End If
Next

End Sub


I had to make a list of radio buttons that would function like a radiobutton list and select the record too. Thus, my code isn't an exact conversion of his but it is close and maintains the same basic functionality.

Generaloops Pin
Specificity22-Jun-05 7:49
sussSpecificity22-Jun-05 7:49 

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.