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

Script GridClientSelectColumn Header Object From Server-Side

4 Answers 109 Views
Grid
This is a migrated thread and some comments may be shown as answers.
codispdp
Top achievements
Rank 1
codispdp asked on 21 Oct 2008, 06:21 PM
How can I check the ClientSelectColumn checked state in the header from CodeBehind on the ItemCommand event?

4 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 22 Oct 2008, 05:42 AM
Hi,

Try using a GridTemplateColumn with a Checkbox in the HeaderTemplate and ItemTemplate for selecting the Grid rows instead of using GridClientSelectColumn. So that you can check the header checkbox in the ItemCommand event. Here I  have added a button in the commandItemTemplate and in its click event I am accessing the header check box and checking it.

ASPX:
<MasterTableView DataSourceID="SqlDataSource1" CommandItemDisplay="top"  > 
     <CommandItemTemplate> 
        <asp:LinkButton ID="LinkButton1" runat="server" Text="Check" CommandName="Check" ></asp:LinkButton> 
    </CommandItemTemplate> 
 
  <Columns> 
 
      <telerik:GridTemplateColumn    HeaderText="TempCol"  DataField="ProductName"  UniqueName="TempCol" > 
       <HeaderTemplate> 
             <asp:CheckBox ID="CheckBox2" AutoPostBack="true" runat="server" /> 
       </HeaderTemplate> 
      <ItemTemplate> 
         <asp:CheckBox ID="CheckBox1"  AutoPostBack="true" runat="server" /> 
      </ItemTemplate> 
     </telerik:GridTemplateColumn> 
  </Columns> 
</MasterTableView> 


CS:

// code to select all rows on clicking the header CheckBox
protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
        foreach (GridHeaderItem header in RadGrid1.MasterTableView.GetItems(GridItemType.Header)) 
        { 
            CheckBox chkbx = (CheckBox)header["SelectCol"].Controls[0]; 
            if (chkbx.Checked) 
            { 
                foreach (GridDataItem item in RadGrid1.MasterTableView.Items) 
                { 
                    item.Selected = true
                } 
            } 
        } 
    } 



CS:

//code to check all rows on clicking the linkbutton in the CommanditemTemplate.

protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
    { 
        if (e.CommandName == "Check") 
        { 
            foreach (GridHeaderItem header in RadGrid1.MasterTableView.GetItems(GridItemType.Header)) 
            { 
                CheckBox chkbx = (CheckBox)header["SelectCol"].Controls[0]; 
                chkbx.Checked = true
            } 
        } 
        
        
    } 


Thanks
Shinu.
0
codispdp
Top achievements
Rank 1
answered on 22 Oct 2008, 06:54 PM
Shinu,

Thanks I have the check/uncheck all  function working now with a HeaderTemplate and ItemTemplate for the dataitems. I am using the RadGrid hierarchy model and the persistence of checked items and header item using ViewState. I have everything working except when a GridDataItem is uncheck in the detailsTable I am trying too access the GridHeaderItem checkbox for that GridDataItem to remove the UniqueID of the header Checkbox control to remove it from the ViewState. Is there a way to access the  details table gridheader from the sender object of the itemtemplate checkbox?

Thanks again

<telerik:RadGrid  
ID="rgProjects"   
runat="server"   
Skin="WebBlue"   
Width="95%"   
CellPadding="1"   
CellSpacing="0"   
PageSize="100"   
AllowPaging="True"   
AllowSorting="True"   
ShowStatusBar="true" 
AllowMultiRowSelection="True"   
AutoGenerateColumns="False">  
<PagerStyle Mode="NumericPages"></PagerStyle> 
<MasterTableView Width="100%" DataKeyNames="Profile_Num" AllowMultiColumnSorting="True" Name=ProjectProfile>  
<Columns> 
<telerik:GridTemplateColumn UniqueName="MasterTemplate" > 
<HeaderTemplate>    
<asp:CheckBox ID="headerChkbox" AutoPostBack="true" runat="server" OnCheckedChanged="ToggleSelectedState" />    
</HeaderTemplate>    
<ItemTemplate> 
<asp:CheckBox ID="cbChecked" Runat="server" AutoPostBack="True" OnCheckedChanged="CheckChanged" /> 
</ItemTemplate> 
</telerik:GridTemplateColumn> 
<telerik:GridBoundColumn SortExpression="PROJECT_NUMBER" HeaderText="Project" HeaderButtonType="TextButton" DataField="PROJECT_NUMBER" /> 
<telerik:GridBoundColumn SortExpression="PROFILE_NUM" HeaderText="Profile" HeaderButtonType="TextButton" DataField="PROFILE_NUM" /> 
<telerik:GridBoundColumn SortExpression="LOGO" HeaderText="Logo" HeaderButtonType="TextButton" DataField="LOGO" /> 
<telerik:GridBoundColumn SortExpression="COORDINATOR_NAME" HeaderText="Coord" HeaderButtonType="TextButton" DataField="COORDINATOR_NAME" /> 
<telerik:GridBoundColumn SortExpression="DESCRIPTION" HeaderText="Profile Description" HeaderButtonType="TextButton" DataField="DESCRIPTION" /> 
<telerik:GridBoundColumn SortExpression="REGION_DESC" HeaderText="Region" HeaderButtonType="TextButton" DataField="REGION_DESC" /> 
<telerik:GridBoundColumn SortExpression="CREATOR" HeaderText="Created By" HeaderButtonType="TextButton" DataField="CREATOR" /> 
</Columns>    
 
<DetailTables> 
<telerik:GridTableView DataKeyNames="TASK_NUMBER" Name="Tasks" Width="100%">  
<Columns> 
<telerik:GridTemplateColumn UniqueName="ChildTemplate" > 
<HeaderTemplate>    
<asp:CheckBox ID="headerChkbox" AutoPostBack="true" runat="server" OnCheckedChanged="ToggleSelectedState" />    
</HeaderTemplate>    
<ItemTemplate> 
<asp:CheckBox id="cbChecked" Runat="server" AutoPostBack="True" OnCheckedChanged="CheckChanged" /> 
</ItemTemplate> 
</telerik:GridTemplateColumn> 
<telerik:GridBoundColumn DataField="TASK_DESCRIPTION_B" HeaderText="Task Description" HeaderButtonType="TextButton" /> 
<telerik:GridBoundColumn DataField="GROUP_ASSIGNED" HeaderText="Grp" HeaderButtonType="TextButton" /> 
<telerik:GridBoundColumn DataField="COMPLETED_FLAG" HeaderText="Cmp" HeaderButtonType="TextButton" /> 
<telerik:GridBoundColumn DataField="USER_COMPLETED" HeaderText="User Completed" HeaderButtonType="TextButton" /> 
<telerik:GridBoundColumn DataField="DATE_COMPLETED" HeaderText="Date Completed" HeaderButtonType="TextButton" /> 
<telerik:GridBoundColumn DataField="ACTIVE_FLAG" HeaderText="Act" HeaderButtonType="TextButton" /> 
<telerik:GridBoundColumn DataField="DATE_ACTIVATED" HeaderText="Date Activated" HeaderButtonType="TextButton" /> 
<telerik:GridBoundColumn DataField="TASK_NUMBER" HeaderText="Task#" HeaderButtonType="TextButton" /> 
<telerik:GridBoundColumn DataField="DATE_CREATED" HeaderText="Date Created" HeaderButtonType="TextButton" /> 
</Columns> 
</telerik:GridTableView> 
</DetailTables> 
</MasterTableView> 
</telerik:RadGrid> 


 
    Private ReadOnly Property ProfilesChecked() As Hashtable  
        Get 
            Dim res As Object = ViewState("_pc")  
            If (res Is NothingThen 
                res = New Hashtable  
                ViewState("_pc") = res  
            End If 
            Return CType(res, Hashtable)  
        End Get 
    End Property 
 
    Private ReadOnly Property TasksChecked() As Hashtable  
        Get 
            Dim res As Object = ViewState("_tc")  
            If (res Is NothingThen 
                res = New Hashtable  
                ViewState("_tc") = res  
            End If 
            Return CType(res, Hashtable)  
        End Get 
    End Property 
 
    Private ReadOnly Property TasksHeaderChecked() As Hashtable  
        Get 
            Dim res As Object = ViewState("_thc")  
            If (res Is NothingThen 
                res = New Hashtable  
                ViewState("_thc") = res  
            End If 
            Return CType(res, Hashtable)  
        End Get 
    End Property 
 
    Protected Sub CheckChanged(ByVal sender As ObjectByVal e As System.EventArgs)  
        Dim sDataKey As String = Nothing 
        Dim target As Hashtable = Nothing 
        Dim sTemplate As String = Nothing 
        Dim box As CheckBox = CType(sender, CheckBox)  
        Dim item As GridDataItem = CType(box.NamingContainer, GridDataItem)  
        Dim hitem As GridHeaderItem = CType(item.NamingContainer, GridHeaderItem)  
 
 
        Select Case item.OwnerTableView.Name  
            Case "ProjectProfile" 
                sTemplate = "MasterTemplate" 
                target = ProfilesChecked  
                sDataKey = "Profile_Num" 
            Case "Tasks" 
                sTemplate = "ChildTemplate" 
                target = TasksChecked  
                sDataKey = "Task_Number" 
        End Select 
 
        If box.Checked Then 
            target(item(sDataKey).Text) = True 
            item.Selected = True 
        Else 
            target.Remove(item(sDataKey).Text)  
            item.Selected = False 
            'TasksHeaderChecked.Clear()  
        End If 
 
    End Sub 
 
    Protected Sub ToggleSelectedState(ByVal sender As ObjectByVal e As EventArgs)  
        Dim sTemplate As String = Nothing 
        Dim sDataKey As String = Nothing 
        Dim target As Hashtable = Nothing 
        Dim box As CheckBox = DirectCast(sender, CheckBox)  
        Dim hItem As GridHeaderItem = DirectCast(box.NamingContainer, GridHeaderItem)  
 
        Select Case hItem.OwnerTableView.Name  
            Case "ProjectProfile" 
                sTemplate = "MasterTemplate" 
                sDataKey = "Profile_Num" 
                target = ProfilesChecked  
            Case "Tasks" 
                sTemplate = "ChildTemplate" 
                sDataKey = "Task_Number" 
                target = TasksChecked  
                If box.Checked Then 
                    TasksHeaderChecked(CType(hItem("ChildTemplate").FindControl("headerChkbox"), CheckBox).UniqueID) = True 
                Else 
                    TasksHeaderChecked.Remove(CType(hItem("ChildTemplate").FindControl("headerChkbox"), CheckBox).UniqueID)  
                End If 
        End Select 
 
        For Each Item As GridDataItem In hItem.OwnerTableView.Items  
            Item.Selected = box.Checked  
            TryCast(Item(sTemplate).Controls(1), CheckBox).Checked = box.Checked  
            If box.Checked Then 
                target(Item(sDataKey).Text) = True 
            Else 
                target.Remove(Item(sDataKey).Text)  
            End If 
        Next 
 
    End Sub 
 
    Private Sub rgProjects_ItemDataBound(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridItemEventArgs) Handles rgProjects.ItemDataBound  
 
        If TypeOf e.Item Is GridDataItem Then 
 
            Dim item As GridDataItem = CType(e.Item, GridDataItem)  
            Dim box As CheckBox = CType(item.FindControl("cbChecked"), CheckBox)  
            Dim isChecked As Object = Nothing 
 
            Select Case item.OwnerTableView.Name  
                Case "ProjectProfile" 
                    isChecked = ProfilesChecked(item("Profile_Num").Text)  
                Case "Tasks" 
                    isChecked = TasksChecked(item("Task_Number").Text)  
            End Select 
 
            If Not (isChecked Is NothingThen 
                item.Selected = CType(isChecked, Boolean) = True 
                box.Checked = CType(isChecked, Boolean) = True 
            End If 
 
        End If 
 
        If (TypeOf e.Item Is GridHeaderItem) And e.Item.OwnerTableView.Name = "Tasks" Then 
            Dim header As GridHeaderItem = CType(e.Item, GridHeaderItem)  
            Dim chkbx As CheckBox = DirectCast(header("ChildTemplate").FindControl("headerChkbox"), CheckBox)  
            chkbx.Checked = TasksHeaderChecked(chkbx.UniqueID)  
        End If 
 
    End Sub 
 


0
Accepted
Shinu
Top achievements
Rank 2
answered on 23 Oct 2008, 05:00 AM
Hi,

Give a try with the following code snippet and see if it helps.

CS:
protected void cbChecked_CheckedChanged(object sender, EventArgs e) 
    { 
        CheckBox chkbx = (CheckBox)sender; 
        GridDataItem childItem = (GridDataItem)chkbx.NamingContainer; 
        GridTableView ChiltTable = (GridTableView)childItem.OwnerTableView; 
        foreach(GridHeaderItem header in ChiltTable.GetItems(GridItemType.Header) ) 
        { 
            CheckBox headChkBx = (CheckBox)header["ChildTemplate"].FindControl("headerChkbox"); 
            headChkBx.Checked = chkbx.Checked; 
        } 
    }  


Thanks
Shinu.
0
codispdp
Top achievements
Rank 1
answered on 23 Oct 2008, 11:12 AM
Shinu,

Perfect that did the trick.

Thanks    

Dan
Tags
Grid
Asked by
codispdp
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
codispdp
Top achievements
Rank 1
Share this question
or