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

Can't access dynamic CheckBox........

3 Answers 80 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Steve
Top achievements
Rank 1
Steve asked on 09 Dec 2010, 05:13 PM
I have this code to dynmically create a checkbox template field and then access the settings of the checkbox:
        SearchResultGrid.Columns.Clear()
        Dim templateColumnName As String = "Delete"
        Dim col2 As Telerik.Web.UI.GridTemplateColumn = New Telerik.Web.UI.GridTemplateColumn
        col2.EditItemTemplate = New MyTemplate(templateColumnName)
        col2.ItemTemplate = New MyTemplate(templateColumnName)
        col2.HeaderText = templateColumnName
        SearchResultGrid.Columns.Add(col2)

    Private Class MyTemplate
        Implements ITemplate
        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
            boolValue = New CheckBox()
            boolValue.ID = "boolValue"
            AddHandler boolValue.DataBinding, _
                       AddressOf boolValue_DataBinding
            boolValue.Enabled = True
            Dim table As New Table()
            container.Controls.Add(table)
            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 Telerik.Web.UI.GridDataItem = DirectCast(cBox.NamingContainer, Telerik.Web.UI.GridDataItem)
            'cBox.Checked = DirectCast((DirectCast(container.DataItem, DataRowView))("Bool"), Boolean)
        End Sub
    End Class

        For Each item As Telerik.Web.UI.GridDataItem In SearchResultGrid.Items
            Dim chk As CheckBox = item.FindControl("boolValue")
            If chk.Checked Then
                Dim j As Integer = 0
            End If
        Next

The problem is that in the For loop chk is getting set to Nothing.   It's not finding the boolValue control.

How do I access the settings of boolValue for each row in the Grid ?

3 Answers, 1 is accepted

Sort by
0
Cori
Top achievements
Rank 2
answered on 09 Dec 2010, 08:08 PM
Hello Steve,

Have you tried this:

item["Delete"].FindControl("boolValue")

The control is inside the column, not the GridDataItem.
0
Steve
Top achievements
Rank 1
answered on 09 Dec 2010, 08:31 PM
I tried this and it doesn't work:
col2.UniqueName = "Delete"

        For Each item As Telerik.Web.UI.GridDataItem In SearchResultGrid.Items
            Dim chk As CheckBox = item("Delete").FindControl("boolValue")
            If chk.Checked Then
                Dim j As Integer = 0
            End If
        Next

chk is still set to Nothing.   Also I inspected item("Delete") and ofund that there were no Controls in it.
0
Princy
Top achievements
Rank 2
answered on 10 Dec 2010, 07:38 AM
Hello Steve,

I have tried the same code and it is working fine when I added the TemplateColumn in Page_Init event handler. When you are dynamically creating column the Column templates must be added in the Page_Init event handler. Check whether you have added the column in Page_Init event handler.

VB.NET:
Protected Sub Page_Init(sender As Object, e As EventArgs)
    SearchResultGrid.Columns.Clear()
    Dim templateColumnName As String = "Delete"
    Dim col2 As New Telerik.Web.UI.GridTemplateColumn()
    col2.EditItemTemplate = New MyTemplate(templateColumnName)
    col2.ItemTemplate = New MyTemplate(templateColumnName)
    col2.HeaderText = templateColumnName
    SearchResultGrid.Columns.Add(col2)
End Sub

For Each item As Telerik.Web.UI.GridDataItem In RadGrid1.Items
    Dim chk As CheckBox = DirectCast(item.FindControl("boolValue"), CheckBox)
    If chk.Checked Then
        Dim j As Integer = 0
    End If
Next


Thanks.
Princy.
Tags
Grid
Asked by
Steve
Top achievements
Rank 1
Answers by
Cori
Top achievements
Rank 2
Steve
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or