This code is for a checkbox, but you can easily adapt it for a RadioButton:
To capture the event for a control placed into a Template column of a
DataGrid, you need to add the following code to your code-behind (this
assumes the checkbox is named "chkDynamic"):
Protected Sub chkDynamic_CheckedChanged(ByVal Sender As Object, ByVal e As
System.EventArgs)
'This sub will be run for every row that has had its value changed
Dim chk As CheckBox = CType(Sender, CheckBox)
Dim item As DataGridItem = CType(chk.NamingContainer, DataGridItem)
' "item" is now a reference to the DataGrid selected row
If chk.Checked Then item.BackColor = Color.Gray
'From here, you could grab some data from this row with:
item.FindControl("ControlName")
'and then you would know which record was selected.
End Sub
Private Sub dg_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles dg.ItemCreated
'This sub runs anytime the DataGrid needs to dynamically create a cotrol
'but we only are interested in rows that may have controls (not header,
footer or pager rows)
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then
Dim chk As CheckBox = CType(e.Item.FindControl("chkDynamic"),
CheckBox)
AddHandler chk.CheckedChanged, AddressOf chkDynamic_CheckedChanged
'This adds a client-side JavaScript event handler.
'You must also have added a client-side JavaScript function called
chkShow(sender)
chk.Attributes.Add("onClick", "chkShow(this," & rowNum.ToString &
")")
End If
End Sub