Checking/Selecting the header template

11 posts, 1 answers
  1. Kishan Gandikota
    Kishan Gandikota avatar
    44 posts
    Member since:
    Sep 2009

    Posted 30 Dec 2009 Link to this post

    Hi Telerik Team,

    I have a very strange requirement from the customer. Customer is using a RAD Grid with GridTemplateColumn used having both HeaderTemplate & ItemTemplate => both having a check-box.

    I just followed this link  to set the checkbox to the RAD Grid.


    Ex: Assume that the RAD Grid is having 3 records with a check-box similiar to the screenshot given in the above link. When we select "HeaderCheckbox", all the item check-boxes are selected which is clear.

    User is now trying to now select all 3 records (item template records) manually. Please keep in mind that Multi-select is allowed for my grid. What customer is asking why is header check-box not selected for this scenario? 

    So in ToggleRowSelection method, I tried to access headertemplate check-box and set the check-box to checked but I am not able to achieve it. I tried with RadGrid1.MasterTableView.Columns[0] and tried to find the checkbox. But I couldn't find it.

    could you please let me know how to access HeaderTemplate check-box from ItemTemplate check-box event handler?
    protected void ToggleRowSelection(object sender, EventArgs e)
    {
    ((sender
     as CheckBox).Parent.Parent as GridItem).Selected = (sender as CheckBox).Checked;
    }


    Regards,
    Kishan G K


    Regards,
    Kishan G K
  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 04 Jan 2010 Link to this post

    Hello Kishan,

    You can try out the following code to access the header checkbox in the CheckedChanged event of the row checkbox:
    c#:
    protected void ToggleRowSelection(object sender, EventArgs e) 
        { 
           ((sender as CheckBox).Parent.Parent as GridItem).Selected = (sender as CheckBox).Checked; 
           GridHeaderItem headerItem = (GridHeaderItem)RadGrid1.MasterTableView.GetItems(GridItemType.Header)[0]; 
           CheckBox chkbx = (CheckBox)headerItem.FindControl("headerChkbox"); 
        } 

    Hope this helps..
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Kishan Gandikota
    Kishan Gandikota avatar
    44 posts
    Member since:
    Sep 2009

    Posted 04 Jan 2010 Link to this post

    Hi Princy,

    This solution works...Thanks a lot!

    Regards,
    Kishan G K
  5. Roselin
    Roselin avatar
    5 posts
    Member since:
    Oct 2011

    Posted 18 Jan 2012 Link to this post

    Hello Princy,

    I am also facing the same issue in my project, please find the issue details below:
    1. While checking header checkbox in the headerItem all the checkbox are selected. this is working fine
    2. but when Unchecking any one checkbox in the Itemcheckbox, the headercheckbox needs to be unchecked, but this is not happening.  Even I am not able to access the headercheckbox inside the headerItem from ItemcheckBox event. The code written to access headerchekcbox is returning nothing.

    Please find the code what we are using in our project.Please help us to solve this.

    Thanks in advance

    Regards
    Roselin

    Protected Sub RadGrid2_HeaderCheckChanged(ByVal sender As Object, ByVal e As EventArgs) 
                 
            Dim headerCheckBox As CheckBox = TryCast(sender, CheckBox) 
            Dim headeritem As GridHeaderItem = DirectCast(headerCheckBox.NamingContainer, GridHeaderItem) 
            Dim parentItem As GridDataItem = DirectCast(headeritem.OwnerTableView.ParentItem, GridDataItem) 
            Dim detailTable As GridTableView = DirectCast(parentItem.ChildItem.NestedTableViews(0), GridTableView) 
            For Each dataItem As GridDataItem In detailTable.Items            
                If headerCheckBox.Checked Then 
                    TryCast(dataItem.FindControl("ItemCheckBox"), CheckBox).Checked = True 
                Else 
                    TryCast(dataItem.FindControl("ItemCheckBox"), CheckBox).Checked = False 
                End If            
                dataItem.Selected = headerCheckBox.Checked 
            Next 
        End Sub  -- This code is working fine 
               
     Protected Sub RadGrid2_ItemCheckChanged(ByVal sender As Object, ByVal e As EventArgs) 
            Dim itemCheckBox As CheckBox = TryCast(sender, CheckBox) 
            Dim gridItem As GridItem = itemCheckBox.Parent.Parent 
            gridItem.Selected = itemCheckBox.Checked 
            Dim headerItem As GridHeaderItem = TryCast(RadGrid2.MasterTableView.GetItems(GridItemType.Header)(0), GridHeaderItem) 
            Dim headerCheckbox As CheckBox = TryCast(headerItem.FindControl("headerChkbox"), CheckBox) 
     End Sub  -- This code is not working fine and getting Nothing as the value  in the headercheckbox

  6. Kishan Gandikota
    Kishan Gandikota avatar
    44 posts
    Member since:
    Sep 2009

    Posted 18 Jan 2012 Link to this post

    Hi Roselin,

    Can you please paste your aspx page that has the definition for RadGrid?

    Regards,
    Kishan G K
  7. Roselin
    Roselin avatar
    5 posts
    Member since:
    Oct 2011

    Posted 18 Jan 2012 Link to this post

    Hi Kishan,

    Thanks for the immediate reply, please find my aspx Radgrid script below:

    <telerik:RadGrid ID="RadGrid2" AllowMultiRowSelection="true" BackColor="LightGray"
                        Width="100%" Height="189px" runat="server" AllowSorting="True" GridLines="None"
                        AutoGenerateColumns="false">
                        <MasterTableView Width="100%" DataKeyNames="SystemId" AllowMultiColumnSorting="True">
                            <DetailTables>
                                <telerik:GridTableView DataKeyNames="SystemItemId" Name="System" Width="95%" AutoGenerateColumns="false"
                                    GridLines="Horizontal">
                                    <Columns>
                                        <telerik:GridTemplateColumn AllowFiltering="false" UniqueName="ClientSelectColumn">
                                            <HeaderTemplate>
                                                <asp:CheckBox ID="headerChkbox" AutoPostBack="True" OnCheckedChanged="RadGrid2_HeaderCheckChanged"
                                                    runat="server"></asp:CheckBox
                                            </HeaderTemplate>
                                            <ItemTemplate>
                                              <asp:CheckBox ID="ItemCheckBox" AutoPostBack="True" OnCheckedChanged="RadGrid2_ItemCheckChanged"
                                                    runat="server"></asp:CheckBox
                                            </ItemTemplate>
                                        </telerik:GridTemplateColumn>
                                        <telerik:GridBoundColumn SortExpression="SystemItemId" HeaderText="Identifier" HeaderButtonType="TextButton"
                                            Display="false" DataField="ID">
                                        </telerik:GridBoundColumn>
                                        <telerik:GridBoundColumn SortExpression="" HeaderText="SystemItem Name" HeaderButtonType="TextButton"
                                            DataField="Name" UniqueName="Name">
                                        </telerik:GridBoundColumn>
                                        <telerik:GridBoundColumn SortExpression="" HeaderText="Description" HeaderButtonType="TextButton"
                                            Display="true" DataField="Description" UniqueName="Description">
                                        </telerik:GridBoundColumn>
                                    </Columns>
                                </telerik:GridTableView>
                            </DetailTables>
                            <Columns>
                                <telerik:GridButtonColumn ConfirmText="Delete this product?" ConfirmDialogType="RadWindow"
                                    ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="Delete" ConfirmDialogHeight="100px"
                                    ConfirmDialogWidth="220px">
                                    <ItemStyle Width="10%" CssClass="MyImageButton" />
                                </telerik:GridButtonColumn>
                                <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn">
                                    <ItemStyle Width="15%" CssClass="MyImageButton" />
                                </telerik:GridEditCommandColumn>
                                <telerik:GridBoundColumn SortExpression="SystemName" HeaderText="System" HeaderButtonType="TextButton"
                                    DataField="SystemName" ReadOnly="true" UniqueName="SystemName">
                                    <HeaderStyle Width="25%" />
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn SortExpression="UserID" HeaderText="User ID" HeaderButtonType="TextButton"
                                    DataField="UserID" UniqueName="UserID">
                                    <HeaderStyle Width="25%" />
                                </telerik:GridBoundColumn>
                                <telerik:GridDateTimeColumn SortExpression="date_granted" HeaderText="Granted" HeaderButtonType="TextButton"
                                    DataField="date_granted" UniqueName="date_granted">
                                    <HeaderStyle Width="20%" />
                                </telerik:GridDateTimeColumn>
                                <telerik:GridDateTimeColumn SortExpression="date_revoked" HeaderText="Revoked" HeaderButtonType="TextButton"
                                    DataField="date_revoked" UniqueName="date_revoked">
                                    <HeaderStyle Width="15%" />
                                </telerik:GridDateTimeColumn>
                            </Columns>
                        </MasterTableView>
                        <ClientSettings EnableRowHoverStyle="true">
                            <Selecting AllowRowSelect="True" />
                        </ClientSettings>
                    </telerik:RadGrid>
  8. Kishan Gandikota
    Kishan Gandikota avatar
    44 posts
    Member since:
    Sep 2009

    Posted 18 Jan 2012 Link to this post

    Ok. Your implementation seems to be using GridTableView. So to access a control inside a grid, here is the procedure...

    GridTableView nestedTableView = (RadGrid2.MasterTableView.Items[0] as GridDataItem).ChildItem.NestedTableViews[0];
    (nestedTableView.FindControl("headerChkbox") as CheckBox)...

    Try with this code. It should work.

    Regards,
    Kishan G K
  9. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 18 Jan 2012 Link to this post

    Hello,

    Try the following code snippet.
    C#:
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
     CheckBox chk = sender as CheckBox;
     GridDataItem dataItem = (GridDataItem)chk.NamingContainer;
     GridTableView tablview = (GridTableView)(dataItem).ChildItem.NestedTableViews[0];
     CheckBox chk1 = (CheckBox)tablview.FindControl("CheckBox2");
    }

    -Shinu.
  10. Roselin
    Roselin avatar
    5 posts
    Member since:
    Oct 2011

    Posted 18 Jan 2012 Link to this post

    Hi Kishan,

    Tried this code also but we get Nothing only in headercheck box access.please find the code below what we tried just now

    Dim gridDataItem As GridDataItem = TryCast(RadGrid2.MasterTableView.Items(0), GridDataItem)   
      
            Dim nestedTableView As GridTableView = gridDataItem.ChildItem.NestedTableViews(0) 
      
            Dim headerCheckbox As CheckBox = TryCast(nestedTableView.FindControl("headerChkbox"), CheckBox

     

    please tell us how to access the headercheck box,...really it is cumbersome

  11. Roselin
    Roselin avatar
    5 posts
    Member since:
    Oct 2011

    Posted 18 Jan 2012 Link to this post

    Hi Shinu,
    Thanks for the reply..we will try this code and let you know
  12. Roselin
    Roselin avatar
    5 posts
    Member since:
    Oct 2011

    Posted 31 Jan 2012 Link to this post

    Hi Shinu/Kishan,

    I have tried the code mentioned below, but I am  not able to get the header template control values in item check change event. I have tried with other scenario as mentioned in the below code, here I am getting only nothing for Headercheckbox control whereas I am getting Itemcheckbox value in the same place. Please check the code and give me any suggestions.

    Thanks,

    Roselin

     

    For i As Integer = 0 To RadGrid2.MasterTableView.Items.Count - 1
                If RadGrid2.MasterTableView.Items(i).Expanded = True Then
                    For index As Integer = 0 To RadGrid2.MasterTableView.Items(i).ChildItem.NestedTableViews(0).Items.Count
                        If index = 0 Then
                            Dim itemcheckBox As CheckBox = TryCast(RadGrid2.MasterTableView.Items(i).ChildItem.NestedTableViews(0).Items(index).FindControl("ItemCheckBox"), CheckBox) - Getting ItemCheckBox value
                            Dim headerChkBox As CheckBox = TryCast(RadGrid2.MasterTableView.Items(i).ChildItem.NestedTableViews(0).Items(index).FindControl("headerChkbox"), CheckBox) - Getting HeaderCheckBox value as Nothing
      
                        End If
                        Exit For
                    Next
      
                End If
            Next
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017