I am attempting to reference some CheckBoxes withing a RadGrid inside a PlaceHolder.
I have a field that I am retrieving called Block that contains a numerice value. Based on the value I am dynamically creating a number of CheckBoxes in a PlaceHolder equal to that value. I have a Button Click method that is trying to reference the CheckBoxes but it is not working correctly.
This line : Dim BlockCheckBox As CheckBox = DirectCast(phBlocksCheckBox.FindControl("BlockCheckBox_" & cbIndex), CheckBox) is not returning a value.
aspx<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:GridBoundColumn DataField="NumberOfBlocks" HeaderText="Block Count" UniqueName="NumberOfBlocks" AllowFiltering="False" Display="False">
<HeaderStyle Width="20px" />
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn DataField="Block" HeaderText="Blocks" UniqueName="BlocksColumn" AllowFiltering="False">
<ItemTemplate>
<asp:PlaceHolder ID="phBlocksCheckBox" runat="server" />
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
<ClientSettings AllowColumnsReorder="True" AllowDragToGroup="True" ReorderColumnsOnClient="True">
<Scrolling UseStaticHeaders="True" />
<Selecting AllowRowSelect="True" />
<ClientEvents OnRowSelected="RowSelected" OnRowDeselected="RowDeselected"></ClientEvents>
</ClientSettings>
</telerik:RadGrid>
vb
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
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