Add LinkButtons dynamically in a RadGrid

Earlier today we published code to add buttons to a web page dynamically. 

This code will do the same thing in a Telerik RadGrid. The technique is similar, with some twists. 

Note below that the grid lines have 0-many 'takeoff' links added. 

 

In the RadGrid, add a GridTemplateColumn with a PlaceHolder control

<telerik:GridTemplateColumn>
    <ItemTemplate>
        <asp:PlaceHolder runat="server" ID="PlaceHolder1"></asp:PlaceHolder>
    </ItemTemplate>
</telerik:GridTemplateColumn>

In the code behind, wire up the column in the ItemCreated event

Private Sub RadGrid1_ItemCreated(sender As Object, e As GridItemEventArgs) Handles RadGrid1.ItemCreated
    Dim strError As String = ""
    Try
        If TypeOf e.Item Is Telerik.Web.UI.GridDataItem Then
            'get a reference to the control
            Dim Placeholder1 As PlaceHolder = CType(e.Item.FindControl("Placeholder1"), PlaceHolder)
 
            'get a reference to a key field
            Dim intMasterNumber As Int32 = RadGrid1.MasterTableView.DataKeyValues(e.Item.ItemIndex)("intMasterNumber")
 
            'lookup the items that we're going to add to the grid
            Dim oDT As New DataTable
            oDT = SPs.fp_FPTakeoff3_SEL_byMasterNumber(intMasterNumber, Session("ConnectionString")).getTable
 
            'loop through the datatable
            For Each oDataRow As DataRow In oDT.Rows
                'create a new link button
                Dim lnk As New LinkButton
                'set its properties
                lnk.Text = String.Format("Takeoff {0}", oDataRow("TakeoffID"))
                lnk.CommandArgument = oDataRow("TakeoffID")
                'add a handler for the click event
                AddHandler lnk.Click, AddressOf LinkHandler
                'add the control to the placeholder
                Placeholder1.Controls.Add(lnk)
 
                'add a 'space', so the links don't touch each other
                Dim lit As New Literal
                lit.Text = " "
                Placeholder1.Controls.Add(lit)
            Next
        End If
 
    Catch ex As ThreadAbortException
    Catch ex As Exception
        ErrorHandler.globalErrorHandler(ex, strError, App.AppName, App.UserName, False)
        Me.lblError.Text = ex.Message
 
    End Try
 
End Sub
Sub LinkHandler(sender As Object, e As EventArgs)
    Dim strError As String = ""
    Try
        'coerse the sender to a LinkButton
        Dim lnk As LinkButton = CType(sender, LinkButton)
 
        'store the value
        Session("TakeoffID") = lnk.CommandArgument
        'redirect
        Response.Redirect(String.Format("Takeoff4.aspx?Path={0}", Session("Path")))
 
 
    Catch ex As ThreadAbortException
    Catch ex As Exception
        ErrorHandler.globalErrorHandler(ex, strError, App.AppName, App.UserName, False)
        Me.lblError.Text = ex.Message
 
    End Try
 
End Sub

 

 

 

 


RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences