using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

public partial class Default3 : System.Web.UI.Page
{
    protected void ToggleRowSelection(object sender, EventArgs e)
    {
        ((sender as CheckBox).NamingContainer as GridItem).Selected = (sender as CheckBox).Checked;
    }
    protected void RadGrid2_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            e.Item.PreRender += new EventHandler(RadGrid2_ItemPreRender);
        }
    }

    private void RadGrid2_ItemPreRender(object sender, EventArgs e)
    {
        ((sender as GridDataItem)["chk"].FindControl("chkSelect") as CheckBox).Checked = (sender as GridDataItem).Selected;
    }

    protected void ToggleSelectedState(object sender, EventArgs e)
    {
        CheckBox headerCheckBox = (sender as CheckBox);
        foreach (GridDataItem dataItem in RadGrid2.MasterTableView.Items)
        {
            (dataItem.FindControl("chkSelect") as CheckBox).Checked = headerCheckBox.Checked;
            dataItem.Selected = headerCheckBox.Checked;
        }
    }

    protected void RadGrid2_PreRender(object sender, EventArgs e)
    {
        GridHeaderItem headerItem = RadGrid2.MasterTableView.GetItems(GridItemType.Header)[0] as GridHeaderItem;
        (headerItem.FindControl("chkCheckUncheckAll") as CheckBox).Checked = RadGrid2.SelectedItems.Count == RadGrid2.Items.Count;
    }
}