This is a migrated thread and some comments may be shown as answers.

[Solved] dynamic imagabuttons added to placeholder in GridTemplateColumn

4 Answers 97 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Morten
Top achievements
Rank 2
Iron
Iron
Iron
Morten asked on 12 Nov 2010, 01:17 PM
I realize it's more of a general asp.net challenge, however...

In a GridTemplateColumn I have a placeholder to which I add one or several imagebuttons. So far so good. My trouble is how to add click handlers to each of these dynamically created imagebuttons? Preferably, I'd would like to handle the click event as I do with imagebuttons added declaratively (in the grid ItemCommand handler)

Any Ideas?

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 12 Nov 2010, 01:27 PM
Hello,


In the ItemCreated event get refference to GridDataItem and then the control by using FindControl() method and attach the event handler.


Thanks,
Princy.
0
Morten
Top achievements
Rank 2
Iron
Iron
Iron
answered on 12 Nov 2010, 01:45 PM
Hi Princy
thanks for your response, however as I create the imagebutton(s) dynamically and add them to the placeholder in the grid ItemCreated handler how do I use the FindContol() here?

My GridTemplateColumn:
<telerik:GridTemplateColumn HeaderText="xxx" UniqueName="xxx">
  <ItemTemplate>
    <asp:PlaceHolder runat="server" ID="plh" />
  </ItemTemplate>
</telerik:GridTemplateColumn>

I'm adding dynamically created imagebuttons to the placeholder "plh" (sometimes none, sometimes several):

grid.ItemCreated
 
  Select Case e.Item.ItemType
    Case GridItemType.AlternatingItem, GridItemType.Item
      Dim dataEntry As HistoryEntity = CType(e.Item.DataItem, HistoryEntity)
      Dim gdi As GridDataItem = e.Item
 
      If dataEntry.ActionCounts > 0 Then
 
        Dim ib As ImageButton
        Dim actions As ActionsCollection = GetActions(dataEntry.Id)
        Dim plhActions As PlaceHolder = gdi.FindControl("plhActions")
 
    For Each a As ActionsEntity In actions
      ib = New ImageButton
      With ib
        .ID = String.Format("ID{0}", a.ActionId)
        .CommandArgument = a.ActionId
        .CommandName = "CompleteAction"
        .ImageUrl = "~/Images/16/check.png"
        AddHandler .Click, AddressOf ib_Click
      End With
      
      plh.Controls.Add(ib)
    Next
    ...
 
Private Sub ib_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs)
...

0
Mira
Telerik team
answered on 17 Nov 2010, 10:48 AM
Hello Morten,

I suggest that you handle the ItemDataBound event as well and use the following code:
grid.ItemCreated
  
  Select Case e.Item.ItemType
    Case GridItemType.AlternatingItem, GridItemType.Item
      Dim dataEntry As HistoryEntity = CType(e.Item.DataItem, HistoryEntity)
      Dim gdi As GridDataItem = e.Item
  
      If dataEntry.ActionCounts > 0 Then
  
        Dim ib As ImageButton
        Dim actions As ActionsCollection = GetActions(dataEntry.Id)
        Dim plhActions As PlaceHolder = gdi.FindControl("plhActions")
  
    For Each a As ActionsEntity In actions
      ib = New ImageButton
      With ib
        .ID = String.Format("ID{0}", a.ActionId)
      End With
      AddHandler ib.Click, AddressOf ib_Click
       
      plh.Controls.Add(ib)
    Next
    ...
  
 
grid.ItemDataBound
  
  Select Case e.Item.ItemType
    Case GridItemType.AlternatingItem, GridItemType.Item
      Dim dataEntry As HistoryEntity = CType(e.Item.DataItem, HistoryEntity)
      Dim gdi As GridDataItem = e.Item
  
      If dataEntry.ActionCounts > 0 Then
  
        Dim ib As ImageButton
        Dim actions As ActionsCollection = GetActions(dataEntry.Id)
        Dim plhActions As PlaceHolder = gdi.FindControl("plhActions")
  
    For Each a As ActionsEntity In actions
      ib = New ImageButton
      With ib
        .ID = String.Format("ID{0}", a.ActionId)
        .CommandArgument = a.ActionId
        .CommandName = "CompleteAction"
        .ImageUrl = "~/Images/16/check.png"       
      End With
      AddHandler ib.Click, AddressOf ib_Click
       
      plh.Controls.Add(ib)
    Next
    ...
 
 
Private Sub ib_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs)
...

Please let me know whether this helps.

Best wishes,
Mira
the Telerik team
Browse the vast support resources we have to jumpstart your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Alain
Top achievements
Rank 1
answered on 30 Jan 2013, 04:40 PM
Mira,

In you sample code, I am correct is assuming that within the ItemCreated handler, e.Item.DataItem would never be available?

Thanks,
Alain.
Tags
Grid
Asked by
Morten
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Princy
Top achievements
Rank 2
Morten
Top achievements
Rank 2
Iron
Iron
Iron
Mira
Telerik team
Alain
Top achievements
Rank 1
Share this question
or