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

FindControl for CommandItem controls

1 Answer 145 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Anthony
Top achievements
Rank 1
Anthony asked on 19 Feb 2009, 06:22 PM
I have two sections on my page:  a set of controls on the top of the page that specify various controls for searching data, and a RadGrid below it that shows the results. 

Inside the CommandItemTemplate of the RadGrid, I have a checkbox.

In an eventhandler not directly related to the grid itself (a button click) I need to get a reference to the Checkbox inside the command item template, but I can't seem to find the right way to get a reference. 

Any suggestions?  Here's a couple of snippets of different ways I tried to get a reference from the button click event. 
The inner part of the for loop doesn't get hit because when inside the button click event handler, the if Item si GridCommandItem is always false. 

<CommandItemTemplate> 
    <asp:CheckBox ID="chkMyCheckBox" Style="float: left" runat="server" Text="My Checkbox" TextAlign="Right" AutoPostBack="true" OnCheckChanged="chkMyCheckBox_CheckChanged" /> 
</CommandItemTemplate> 
 
Dim chk as CheckBox = DirectCast(dgMyGrid.FindControl("chkMyCheckBox"),CheckBox) 
 
 
            For Each Item As GridItem In dgMyGrid.Items 
                If Item Is GetType(Telerik.Web.UI.GridCommandItem) Then 
                    Dim CommandItem As GridCommandItem = DirectCast(Item, GridCommandItem) 
                    Dim Header As GridTHead = DirectCast(CommandItem.NamingContainer, GridTHead) 
                    Dim chk1 As CheckBox = DirectCast(Header.FindControl("chkMyCheckBox"), CheckBox) 
                    Dim chk2 As CheckBox = DirectCast(CommandItem.FindControl("chkMyCheckBox"), CheckBox) 
                End If 
            Next 
 

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 20 Feb 2009, 03:57 AM
Hello Anthony,

Try out the following code to access the checkbox in the CommandItemTemplate of the grid on a button click event:
vb:
     Protected Sub Button1_Click1(ByVal sender As Object, ByVal e As EventArgs) 
         Dim cmdItem As GridCommandItem = DirectCast(RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)(0), GridCommandItem) 
         Dim chk As CheckBox = DirectCast(cmdItem.FindControl("chkMyCheckBox"), CheckBox) 
     End Sub 

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