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

Posted on 2005-05-16
Last Modified: 2007-12-19
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.

Question by:Coolcld
    LVL 8

    Expert Comment

    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?


    Accepted Solution


    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?

    LVL 26

    Expert Comment

    post a request in the community support ..

    Author Comment

    yes Please.


    Expert Comment

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


    Author Comment

    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.

    Expert Comment

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now