[Solved] Referencing Dynamically Generated PlaceHolder CheckBox Returns Nothing

0 Answers 1 View
CheckBox Grid
Wei
Top achievements
Rank 1
Iron
Wei asked on 26 Feb 2026, 07:20 PM

Language : vb

I have a table that contains a numeric field. Using this field value I generate variable numbers of CheckBoxes in a RadGrid PlaceHolder :

I am attempting reference the CheckBoxes in the selected row and do some processing if the a CheckBox is checked.

I can get the reference to the PlaceHolder : Dim phBlocksCheckBox As PlaceHolder = DirectCast(item.FindControl("phBlocksCheckBox"), PlaceHolder)

But the code to return the CheckBox reference is returning Nothing : Dim BlockCheckBox As CheckBox = DirectCast(phBlocksCheckBox.FindControl("BlockCheckBox_" & cbIndex), CheckBox)

Here is my code :

apsx :
    <div style="clear: both">
        <div style="float: right; margin-right: 10px;">
            <asp:Button ID="GenerateButton" runat="server" Text="Generate Cassette Labels" Disabled="True"/>
        </div>
    </div>

    <telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" AllowSorting="True" GridLines="None" AllowMultiRowSelection="True" AllowFilteringByColumn="True" EnableLinqExpressions="False">
        <MasterTableView AutoGenerateColumns="False">
            <PagerStyle AlwaysVisible="True"></PagerStyle>
            <Columns>
                <telerik:GridClientSelectColumn UniqueName="checkboxColumn1" HeaderTooltip="SELECT All" ShowSortIcon="False" ShowFilterIcon="False" Reorderable="False">
                    <HeaderStyle Width="30px" />
                </telerik:GridClientSelectColumn>
                <telerik:GridBoundColumn DataField="NumberOfBlocks" HeaderText="Block Count" UniqueName="NumberOfBlocks" AllowFiltering="False" Display="False">
                    <HeaderStyle Width="20px" />
                </telerik:GridBoundColumn>
                <telerik:GridTemplateColumn DataField="Block" HeaderText="Blocks" UniqueName="Blocks" AllowFiltering="False">
                    <ItemTemplate>
                        <asp:PlaceHolder ID="phBlocksCheckBox" runat="server" />
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>


vb :
    Public dt As DataTable = Nothing
    Protected Sub RadGrid_NeedDataSource(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
        If dt Is Nothing Then
            Dim objRadGrid As RadGrid = CType(sender, RadGrid)
            Dim objCommand As New MySqlCommand
            dt = New DataTable()

 
            objCommand.CommandText = "SELECT Block AS NumberOfBlocks FROM samples"
            Call DBClass.TapDB("ConnStr", objCommand, "getRS", "", dt)

            objRadGrid.DataSource = dt
        End If
    End Sub

    Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs) Handles RadGrid1.ItemDataBound
        'Ensure we are dealing with a data item (not header, footer, etc.)
        If TypeOf e.Item Is GridDataItem Then
            Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)

            'Find the placeholder control defined in the ItemTemplate
            Dim phBlocksCheckBox As PlaceHolder = CType(item.FindControl("phBlocksCheckBox"), PlaceHolder)

            If phBlocksCheckBox IsNot Nothing Then
                Dim NumberOfBlocks As Integer = DataBinder.Eval(item.DataItem, "NumberOfBlocks")

                'Dynamically create and add checkboxes
                If NumberOfBlocks > 0 Then
                    For BlockIndex As Integer = 0 To NumberOfBlocks - 1
                        Dim BlockCheckBox As New CheckBox()
                        BlockCheckBox.ID = "BlockCheckBox_" & (BlockIndex + 1).ToString()
                        BlockCheckBox.Text = (BlockIndex + 1).ToString()
                        BlockCheckBox.Checked = True
                        phBlocksCheckBox.Controls.Add(BlockCheckBox)
                        phBlocksCheckBox.Controls.Add(New LiteralControl("    "))
                    Next
                End If
            End If
        End If
    End Sub


    Protected Sub Button_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles GenerateCassetteLabelsButton.Click
        Dim NumberOfBlocks As Integer = 0

        For Each item As GridDataItem In RadGrid1.SelectedItems
            NumberOfBlocks = CInt(item("NumberOfBlocks").Text)

            Dim phBlocksCheckBox As PlaceHolder = DirectCast(item.FindControl("phBlocksCheckBox"), PlaceHolder)
            If phBlocksCheckBox IsNot Nothing Then
HttpContext.Current.Response.Write("phBlocksCheckBox IsNot Nothing<br><br>")
                For cbIndex As Integer = 1 To NumberOfBlocks
HttpContext.Current.Response.Write("-BlockCheckBoxID : " & "BlockCheckBox_" & cbIndex & "<br><br>")
                    Dim BlockCheckBox As CheckBox = DirectCast(phBlocksCheckBox.FindControl("BlockCheckBox_" & cbIndex), CheckBox)

                    If BlockCheckBox IsNot Nothing Then
HttpContext.Current.Response.Write("BlockCheckBox IsNot Nothing<br><br>")
                        'Do something with BlockCheckBox.Text
HttpContext.Current.Response.Write("Block : " & BlockCheckBox.Text & "<br><br>")
                    End If
                Next
            End If
        Next

        RadGrid1.Rebind()
    End Sub

 

No answers yet. Maybe you can help?

Tags
CheckBox Grid
Asked by
Wei
Top achievements
Rank 1
Iron
Share this question
or