New to Telerik UI for ASP.NET AJAXStart a free 30-day trial

Persisting CheckBox Control State in GridTemplateColumn on Rebind

There are cases in which the user would like to retain the state for checkbox control inside GridTemplateColumn on grid operation which requires rebinding. In such situation you can use ViewState property on the page which holds the grid to save the current checkbox selection. The alteration of this property should be made in the OnCheckedChanged event handler of the respective checkbox.

The forthcoming sample presents how to retain the checkbox state in a hierarchical grid after expand/collapse for further custom operations. There are separate ViewState properties for each master/detail table. The example below is optimized for ViewState minimization as these properties are modified only when CheckBoxInstance.Checked is true. Otherwise the row value is not added to the ViewState.

Similar actions could be performed for persistence of other controls state in GridTemplateColumn.

ASP.NET
<telerik:RadGrid RenderMode="Lightweight" ID="RadGrid1" runat="server" AutoGenerateColumns="False" AllowSorting="True"
  AllowPaging="True" PageSize="3">
  <MasterTableView DataMember="Customers" PageSize="3" AllowPaging="True">
    <PagerStyle Mode="NumericPages"></PagerStyle>
    <Columns>
      ..............
      <telerik:GridBoundColumn UniqueName="CustomerID" ReadOnly="True" Display="False"
        DataField="CustomerID" />
      <telerik:GridTemplateColumn UniqueName="MasterTemplate" HeaderText="Checkbox column 1">
        <ItemTemplate>
          <asp:CheckBox ID="cbChecked" runat="server" AutoPostBack="True" OnCheckedChanged="CheckChanged">
          </asp:CheckBox>
        </ItemTemplate>
      </telerik:GridTemplateColumn>
      ..............
    </Columns>
    <DetailTables>
      <telerik:GridTableView DataMember="Customers1" PageSize="3" AllowPaging="True">
        <Columns>
          ...................
          <telerik:GridBoundColumn UniqueName="CustomerID" ReadOnly="True" Display="False"
            DataField="CustomerID" />
          <telerik:GridTemplateColumn UniqueName="Child1Template" HeaderText="Checkbox column 2">
            <ItemTemplate>
              <asp:CheckBox ID="cbChecked" runat="server" AutoPostBack="True" OnCheckedChanged="CheckChanged">
              </asp:CheckBox>
            </ItemTemplate>
          </telerik:GridTemplateColumn>
          ....................
        </Columns>
        <DetailTables>
          <telerik:GridTableView DataMember="Customers2" PageSize="3" AllowPaging="True">
            <Columns>
              .....................
              <telerik:GridBoundColumn UniqueName="CustomerID" ReadOnly="True" Display="False"
                DataField="CustomerID" />
              <telerik:GridTemplateColumn UniqueName="Child2Template" HeaderText="Checkbox column 3">
                <ItemTemplate>
                  <asp:CheckBox ID="cbChecked" runat="server" AutoPostBack="True" OnCheckedChanged="CheckChanged">
                  </asp:CheckBox>
                </ItemTemplate>
              </telerik:GridTemplateColumn>
              ......................
            </Columns>
          </telerik:GridTableView>
        </DetailTables>
      </telerik:GridTableView>
    </DetailTables>
  </MasterTableView>
</telerik:RadGrid>
Not finding the help you need?
Contact Support