• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 688
  • Last Modified:

Add handlers to dynamically created radio buttons in a datagrid template column

I have a datagrid in a usercontrol with dynamically created template columns holding radio buttons in the cells. Both the rows and columns of datagrid are dynamic. Data is bound like this
                dgResults.DataSource = objDataSet

Now I want to add event handler to the checkedchanged event of each radio button. I am doing it in 'dgResults_ItemCreated' event like

Dim radCol As RadioButton
Dim strMapName As String

 radCol = CType(e.Item.Cells(intCnt).FindControl("rad" & strMapName), RadioButton)
                        If Not radCol Is Nothing Then
                            AddHandler radCol.CheckedChanged, AddressOf RadioButton_Click
                      End if

I have defined the method 'RadioButton_Click' also.
But the event is never getting hit. What should I do in this scenario? Any reply will be greatly helpful.

Thanks in advance.

1 Solution
Hi Coolcld,

Are you sure that the code where the handler is added is executing?
Have you tried stepping through your code to ensure that it does indeed get added?

CoolcldAuthor Commented:

David, thanks for the reply.

I got it working by setting autopostback=true and by modifying some parts of code.  
I want to close/delete/re-take the question now. What should I do?

post a request in the community support .. http://www.experts-exchange.com/Community_Support/
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

CoolcldAuthor Commented:
yes Please.

What "parts of the code" did you modify?  I'm having the exact problem you are!!!!!

CoolcldAuthor Commented:
Hi lyntron4545 ,

I created the radio buttons dynamically using the interface ITemplate.
In the 'ITemplate.InstantiateIn' event, I created the control, assigned id and set autopostback=true.
As I mentioned in the qtn, I added the addhandler in itemcreated event.
It worked fine.

I don't remember what other changes I made to make this work but I guess just setting autopostback=true made it work.
Thanks for the reply Coolcd.

It appears that on my ItemCreated the code is trying to recreate my dynamic controls on postback and I receive an error of, "Multiple controls with the same ID '_ctl0' were found. FindControl requires that controls have unique IDs."  Do you have any ideas?

Thanks again I'm in a big bind.

Here is my code:
Public Class DataGridTemplate
    Implements ITemplate
    Dim templateType As ListItemType
    Dim columnName As String
    Public Shared intRadioButton As Integer = 1

    Sub New(ByVal type As ListItemType, ByVal ColName As String)
        templateType = type
        columnName = ColName
    End Sub

    Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
        Dim rbutton As RadioButton

        rbutton = New RadioButton
        rbutton.ID = "rad" & intRadioButton 'global
        rbutton.AutoPostBack = True
        intRadioButton += 1
    End Sub
End Class
. . .
AddHandler dgClearPartID.ItemCreated, AddressOf Me.dgClearPartID_ItemCreated

dgClearPartID.DataSource = dsDataSet
. . .
Public Sub dgClearPartID_ItemCreated(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
        If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
            Dim radCol As New RadioButton
            Dim intTempClass As Integer = DataGridTemplate.intRadioButton - 1

            radCol = CType(e.Item.Cells(0).FindControl("rad" & intTempClass), RadioButton)
            If Not radCol Is Nothing Then
                AddHandler radCol.CheckedChanged, AddressOf Me.RadioButton_Click
            End If
        End If
    End Sub
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now