trxtbox ItemTemplate in GridTemplateColumn programatically

2 posts, 0 answers
  1. Kishor
    Kishor avatar
    82 posts
    Member since:
    Oct 2012

    Posted 17 Jun 2014 Link to this post

    I want to add GridTemplateColumn and Textbox as itemtemplate programatically.
    I used the below code but it just generate the GridTemplate columns with blank rows without textbox as shown in attached image
    Please help me to solve this problem.

    Sub btnFurnaceAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFurnaceAdd.Click
            num = gdFurnace.Items.Count + 1
            Dim dt As New DataTable()
           gdFurnace.DataSource = addBlankLines(dt, num)
                      For i = 0 To 8
                   Dim col As New GridTemplateColumn
                    col.HeaderText = "Column" + i.ToString
                   col.UniqueName = i.ToString
                   col.ItemTemplate = New MyTemplate(col.UniqueName)
                   col.HeaderStyle.Font.Bold = True
    End Sub

    ​ Private Function addBlankLines(ByVal dt As DataTable, ByVal num As Integer) As DataTable
    Dim count As Integer = 0
            If num <> 0 Then
                Dim drBlank As DataRow = Nothing
                For i As Integer = 0 To num - 1
                    drBlank = dt.NewRow()

            End If
            Return dt

        End Function

    ​Private Class MyTemplate
    Implements ITemplate
            Protected lControl As LiteralControl
            Protected validatorTextBox As RequiredFieldValidator
            Protected searchGoogle As HyperLink
            Protected textBox As TextBox
            Protected boolValue As CheckBox
            Private colname As String

            Public Sub New(ByVal cName As String)
    colname = cName
            End Sub
            Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements ITemplate.InstantiateIn
    lControl = New LiteralControl()
                lControl.ID = "lControl"
                AddHandler lControl.DataBinding, AddressOf lControl_DataBinding
                textBox = New TextBox()
                textBox.ID = "templateColumnTextBox"
                validatorTextBox = New RequiredFieldValidator()
                validatorTextBox.ControlToValidate = "templateColumnTextBox"
                validatorTextBox.ErrorMessage = "*"
                searchGoogle = New HyperLink()

    searchGoogle.ID = "searchGoogle"
                AddHandler searchGoogle.DataBinding, _
                           AddressOf searchGoogle_DataBinding
                boolValue = New CheckBox()
                boolValue.ID = "boolValue"
                AddHandler boolValue.DataBinding, _
                           AddressOf boolValue_DataBinding
                boolValue.Enabled = False
                Dim table As New Table()

    Dim row1 As New TableRow()
                Dim row2 As New TableRow()
                Dim cell11 As New TableCell()
                Dim cell12 As New TableCell()
                Dim cell21 As New TableCell()
                Dim cell22 As New TableCell()

                cell11.Text = colname + ": "
                cell21.Text = "Search Google for: "

    container.Controls.Add(New LiteralControl("<br />"))

            End Sub
            Sub boolValue_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
    Dim cBox As CheckBox = DirectCast(sender, CheckBox)
                Dim container As GridDataItem = DirectCast(cBox.NamingContainer, GridDataItem)
                cBox.Checked = DirectCast((DirectCast(container.DataItem, DataRowView))("Bool"), Boolean)

            End Sub
            Sub searchGoogle_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
    Dim link As HyperLink = DirectCast(sender, HyperLink)
                Dim container As GridDataItem = DirectCast(link.NamingContainer, GridDataItem)
                link.Text = (DirectCast(container.DataItem, DataRowView))(colname).ToString()
                link.NavigateUrl = "" + _
                       (DirectCast(container.DataItem, DataRowView))("ContactName").ToString() + "&btnG=Google+Search"

            End Sub
            Public Sub lControl_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
    Dim l As LiteralControl = DirectCast(sender, LiteralControl)
                Dim container As GridDataItem = DirectCast(l.NamingContainer, GridDataItem)
                l.Text = (DirectCast(container.DataItem, DataRowView))(colname).ToString() + "<br />"

            End Sub
        End Class
  2. Eyup
    Eyup avatar
    2942 posts

    Posted 23 Jun 2014 Link to this post

    Hi Sawan,

    Can you please verify that you are creating the grid in Page_Init as explained in this article?

    I am also sending a sample with programmatically created RadGrid. Please run the attached application and let me know if it helps you.


    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

  3. DevCraft R3 2016 release webinar banner
Back to Top