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

CheckBox values from Grid with paging

1 Answer 107 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Adnan
Top achievements
Rank 1
Adnan asked on 09 Feb 2015, 10:53 AM
Hi,

i have a problem with picking up Check Box values from the Data Grid, where the option sorting, paging, etc. is allowed.

My ASPX code is shown below:

<telerik:RadGrid ID="gvUsers" EnableViewState="true" runat="server" AllowFilteringByColumn="True" AllowPaging="True" AllowSorting="True" CellSpacing="0" OnSortCommand="gvUsers_SortCommand" OnGroupsChanging="gvUsers_GroupsChanging"
    OnPageIndexChanged="gvUsers_PageIndexChanged" OnPreRender="gvUsers_PreRender" ShowGroupPanel="True" OnPageSizeChanged="gvUsers_PageSizeChanged" PageSize="2" ShowFooter="True" Skin="Sunset" style="table-layout:fixed;" AutoGenerateColumns="False" GridLines="None" OnNeedDataSource="gvUsers_NeedDataSource">
    <ClientSettings AllowDragToGroup="True">
    </ClientSettings>
    <GroupingSettings CaseSensitive="false" />
    <GroupPanel Text="<%$Resources:localization, lblGroupingText%>"></GroupPanel>
    <MasterTableView DataKeyNames="UserID" AutoGenerateColumns="False" ClientDataKeyNames="UserID" Width="100%">
        <Columns>
            <telerik:GridTemplateColumn UniqueName="cbSelect" AllowFiltering="false" ShowFilterIcon="false">
                <ItemTemplate>
                  <asp:CheckBox ID="cbSelectUser" runat="server" OnCheckedChanged="cbSelectUser_CheckedChange"
                    AutoPostBack="True" />
                </ItemTemplate>
            </telerik:GridTemplateColumn>                                                    
            <telerik:GridBoundColumn DataField="UserID" FilterControlAltText="Filter ID column" HeaderText="ID" ReadOnly="True" SortExpression="UserID" UniqueName="UserID" CurrentFilterFunction="EqualTo" AutoPostBackOnFilter="true" ShowFilterIcon="false" HeaderStyle-Width="30px" FilterControlWidth="30px"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="FullName" FilterControlAltText="Filter Full name column" HeaderText="<%$Resources:localization, lblUserFullName%>" SortExpression="FullName" UniqueName="FullName" CurrentFilterFunction="Contains" AutoPostBackOnFilter="true" ShowFilterIcon="false" HeaderStyle-Width="150px" FilterControlWidth="145px"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="Email" FilterControlAltText="Filter email" HeaderText="<%$Resources:localization, lblEmail%>" SortExpression="Email" UniqueName="Email" CurrentFilterFunction="Contains" AutoPostBackOnFilter="true" ShowFilterIcon="false" HeaderStyle-Width="150px" FilterControlWidth="145px"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="RoleName" FilterControlAltText="Filter role column" HeaderText="<%$Resources:localization, lblUserRole%>" SortExpression="RoleName" UniqueName="RoleName" CurrentFilterFunction="Contains" AutoPostBackOnFilter="true" ShowFilterIcon="false"  HeaderStyle-Width="200px" FilterControlWidth="230px"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="StatusDescription" FilterControlAltText="Filter Status column" HeaderText="<%$Resources:localization, lblStatus%>" SortExpression="StatusDescription" UniqueName="StatusDescription" CurrentFilterFunction="Contains" AutoPostBackOnFilter="true" ShowFilterIcon="false"  HeaderStyle-Width="100px" FilterControlWidth="80px"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="Created" FilterControlAltText="Filter date created column" HeaderText="<%$Resources:localization, lblDateCreated%>" SortExpression="Created" UniqueName="Created" AllowFiltering="false" AutoPostBackOnFilter="true" ShowFilterIcon="false"  HeaderStyle-Width="100px" FilterControlWidth="80px"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="Modified" FilterControlAltText="Filter date modified column" HeaderText="<%$Resources:localization, lblDateModified%>" SortExpression="Modified" UniqueName="Modified" AllowFiltering="false" AutoPostBackOnFilter="true" ShowFilterIcon="false"  HeaderStyle-Width="100px" FilterControlWidth="80px"></telerik:GridBoundColumn>
        </Columns>
        <PagerStyle AlwaysVisible="True"/>
    </MasterTableView>
</telerik:RadGrid>

Because the paging does not persist the checked Check Box values, i have additional two methods which stores  and sets the checked values on page change: PreRender and OnCheckedChange:

protected void cbSelectUser_CheckedChange(object sender, EventArgs e)
{
    ArrayList selectedItems;
 
    if (Session[Constants.SelectedGridItems] == null)
    {
        selectedItems = new ArrayList();
    }
    else
    {
        selectedItems = (ArrayList)Session[Constants.SelectedGridItems];
    }
 
    foreach (GridDataItem item in gvUsers.MasterTableView.Items)
    {
        CheckBox chkSelected = (CheckBox)item.FindControl("cbSelectUser");
        String Id = item["UserID"].Text;
 
        if (chkSelected.Checked)
        {
            if (!selectedItems.Contains(Id))
            {
                selectedItems.Add(Id);
            }
            Session[Constants.SelectedGridItems] = selectedItems;
        }
        else
        {
            selectedItems.Remove(Id);
            Session[Constants.SelectedGridItems] = selectedItems;
        }
    }
}

 and

protected void gvUsers_PreRender(object sender, EventArgs e)
{
    if (Session[Constants.SelectedGridItems] != null)
    {
        ArrayList selectedItems = (ArrayList)Session[Constants.SelectedGridItems];
        Int16 stackIndex;
        for (stackIndex = 0; stackIndex <= selectedItems.Count - 1; stackIndex++)
        {
            string curItem = selectedItems[stackIndex].ToString();
 
            foreach (GridDataItem item in gvUsers.MasterTableView.Items)
            {
                CheckBox box = (CheckBox)item["cbSelect"].Controls[1];
 
                if (curItem.Equals(item.OwnerTableView.DataKeyValues[item.ItemIndex]["UserID"].ToString()))
                {
                    box.Checked = true;
                }
            }
        }
    }
}

On the Page, i also have an Button which is outside the Grid. On the Button_Click it should pick up all Check Box values from the grid, not only those from current page. I tried it with "AllowPaging = false" and Rebind(), but after the rebind method is executed, no one Check box is checked...

How to get it work?

Thanks, Adnan

1 Answer, 1 is accepted

Sort by
0
Adnan
Top achievements
Rank 1
answered on 09 Feb 2015, 11:52 AM
Hi again,

i think i got it. Maybe not the best solution, but just read the values from the Session object, without any AllowPaging and Rebind...
Tags
Grid
Asked by
Adnan
Top achievements
Rank 1
Answers by
Adnan
Top achievements
Rank 1
Share this question
or