Single Selection RadioButton on GridView

2013 年 11 月 26 日4420

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.

0 0