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

trxtbox ItemTemplate in GridTemplateColumn programatically

1 Answer 44 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Kishor
Top achievements
Rank 2
Kishor asked on 18 Jun 2014, 04:23 AM
hello
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.

Protected
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)
       gdFurnace.Rebind()
                  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
               gdFurnace.MasterTableView.Columns.Add(col)
            Next
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()
                dt.Rows.Add(drBlank)

       
     Next
        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()
            row1.Cells.Add(cell11)
            row1.Cells.Add(cell12)
            row2.Cells.Add(cell21)

           
row2.Cells.Add(cell22)
            table.Rows.Add(row1)
            table.Rows.Add(row2)
            cell11.Text = colname + ": "
            cell12.Controls.Add(lControl)
            cell21.Text = "Search Google for: "
            cell22.Controls.Add(searchGoogle)
            container.Controls.Add(textBox)
            container.Controls.Add(validatorTextBox)
            container.Controls.Add(table)

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

        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 = "http://www.google.com/search?hl=en&q=" + _
                   (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

1 Answer, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 23 Jun 2014, 07:15 AM
Hi Sawan,

Can you please verify that you are creating the grid in Page_Init as explained in this article?
http://www.telerik.com/help/aspnet-ajax/grid-programmatic-creation.html#Section22

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

Regards,
Eyup
Telerik
 

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.

 
Tags
General Discussions
Asked by
Kishor
Top achievements
Rank 2
Answers by
Eyup
Telerik team
Share this question
or