[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2005-05-16
11
Medium Priority
?
655 Views
Last Modified: 2007-12-19
Hi,
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
                dgResults.DataBind()


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.


0
Comment
Question by:Coolcld
7 Comments
 
LVL 8

Expert Comment

by:doobdave
ID: 14008750
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?

David
0
 

Accepted Solution

by:
Coolcld earned 0 total points
ID: 14008839
Hi,

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?

Thanks.
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 14009368
post a request in the community support .. http://www.experts-exchange.com/Community_Support/
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:Coolcld
ID: 14016770
yes Please.

Thanks!
0
 

Expert Comment

by:lyntron4545
ID: 15149675
What "parts of the code" did you modify?  I'm having the exact problem you are!!!!!

Thanks
0
 

Author Comment

by:Coolcld
ID: 15153409
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.
0
 

Expert Comment

by:lyntron4545
ID: 15155277
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
        container.Controls.Add(rbutton)
    End Sub
End Class
------------------------------------------------------------------------------------------------------------------
Page_Load()
. . .
AddHandler dgClearPartID.ItemCreated, AddressOf Me.dgClearPartID_ItemCreated

dgClearPartID.DataSource = dsDataSet
dgClearPartID.DataBind()
. . .
------------------------------------------------------------------------------------------------------------------
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
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month19 days, left to enroll

834 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