locked
Single Selection RadioButton on GridView RRS feed

  • Question

  • User-320067883 posted

    Hi! Just want to share some codes to our fellow users. I've just read some post regarding this topic and I didnt see any short way to do it so I think about how I can do this on my own. (just happy as a first timer on ASP ^_^ )

    If your going to use a gridview and u want to put a radio button on a TemplateField to act as a pointer to your selection just use this code on the RadioButton_CheckedChange Event(). By the way, I've used a temporary template field w/c is column 8 (u can set it depending on your grid) where i can put the reference value of my radio buttons that has been clicked

    Protected Sub RadioSel_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim x, y, ctr, ref As Integer

            y = 0
            ctr = 0
            'Get True Radio

            'Get the value of reference and check if theres a radio button already checked
            For x = 0 To GridView1.Rows.Count - 1
                If GridView1.Rows(x).Cells(8).Text <> "" Then
                    ctr = ctr + 1
                    ref = x
                End If
            Next

            'No Existing Radio Button has been clicked for value of 0
            If ctr = 0 Then
                For x = 0 To GridView1.Rows.Count - 1
                    Dim RadioSel As RadioButton = GridView1.Rows(x).FindControl("RadioSel")
                    If RadioSel.Checked = True And GridView1.Rows(x).Cells(8).Text = "" Then
                        GridView1.Rows(x).Cells(8).Text = x
                    End If
                Next
            Else
                Dim RadioSel As RadioButton = GridView1.Rows(ref).FindControl("RadioSel")
                RadioSel.Checked = False
                GridView1.Rows(ref).Cells(8).Text = ""
               
            End If

           'Unchecked the Last Button and Check the New one
            For x = 0 To GridView1.Rows.Count - 1
                Dim RadioSel As RadioButton = GridView1.Rows(x).FindControl("RadioSel")
                If RadioSel.Checked = True And GridView1.Rows(x).Cells(8).Text = "" Then
                    GridView1.Rows(x).Cells(8).Text = x
                End If
            Next

    End Sub 

     If theres any problem just let me know, ok? hope this code can help newbies out there like me.

    Monday, December 4, 2006 12:52 AM

All replies

  • User-119329209 posted

    This solution is a little neater and negates the need to save the old selected value.

    protected void rbSelector_CheckedChanged(object sender, System.EventArgs e)
    {
        //Clear the existing selected row 
        foreach (GridViewRow oldrow in GridView1.Rows)
        {
            ((
    RadioButton)oldrow.FindControl("rbSelector")).Checked = false;
        }

        //Set the new selected row
        RadioButton rb = (RadioButton)sender;
        GridViewRow row = (GridViewRow)rb.NamingContainer;
        ((
    RadioButton)row.FindControl("rbSelector")).Checked = true;
    }

    Charlie

    Thursday, January 3, 2008 5:44 AM
  • User492809006 posted

     Please use client side radio button instead of server side control it will automatically select only one at a time.

     Thanks

    Mrutyunjay palai

    Tuesday, December 9, 2008 5:57 AM
  • User1689678569 posted

     tnx Charlie

    it's working

    Thursday, May 28, 2009 6:48 AM
  • User-1827474539 posted

    Thank  a lot for any idea , it work good , i have changed to VB for someone, who not use C#


    Protected Sub RadioButton1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim dr As GridViewRow
            Dim oldRow As RadioButton = GridView1.FindControl("RadioButton1")

            Dim count = 0
            TextBox3667.Text = Request.Form("RDI")
            For Each dr In GridView1.Rows
                oldRow = CType(GridView1.Rows(count).FindControl("RadioButton1"), CheckBox)
                oldRow.Checked = False
                count += 1

            Next
            'ADD Old Value
            Dim NewRow As RadioButton = sender
            Dim row As GridViewRow = NewRow.NamingContainer
            NewRow.FindControl("RadioButton1")
            NewRow.Checked = True
          
        End Sub

    Tuesday, May 18, 2010 3:36 AM