<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RadGridSelectionSort.aspx.cs" Inherits="RadGridSelectionSort" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
            <Scripts>
                <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" />
                <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" />
                <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js" />
            </Scripts>
        </telerik:RadScriptManager>
        <telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
            <script type="text/javascript">
                var selected = {};
                function RadGrid1_RowSelected(sender, args) {
                    var orderID = args.getDataKeyValue("OrderID");
                    if (!selected[orderID]) {
                        selected[orderID] = true;
                    }
                }
                function RadGrid1_RowDeselected(sender, args) {
                    var orderID = args.getDataKeyValue("OrderID");
                    if (selected[orderID]) {
                        selected[orderID] = null;
                    }
                }
                function RadGrid1_RowCreated(sender, args) {
                    var orderID = args.getDataKeyValue("OrderID");
                    if (selected[orderID]) {
                        args.get_gridDataItem().set_selected(true);
                    }
                }
                function GridCreated(sender, eventArgs) {
                    var masterTable = sender.get_masterTableView();
                    var selectColumn = masterTable.getColumnByUniqueName("SelectColumnName");
                    var headerCheckBox = $(selectColumn.get_element()).find("[type=checkbox]")[0];

                    if (headerCheckBox) {
                        headerCheckBox.checked = masterTable.get_selectedItems().length ==
                            masterTable.get_dataItems().length;
                    }
                }
            </script>
        </telerik:RadScriptBlock>
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
            <AjaxSettings>
                <telerik:AjaxSetting AjaxControlID="RadGrid1">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </AjaxSettings>
        </telerik:RadAjaxManager>
        <telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" CellSpacing="0" AllowSorting="true"
            DataSourceID="SqlDataSource1" GridLines="None" Width="800px" AllowMultiRowSelection="true" AutoGenerateEditColumn="true">
            <ClientSettings>
                <Selecting AllowRowSelect="true" />
                <ClientEvents OnRowCreated="RadGrid1_RowCreated" OnRowSelected="RadGrid1_RowSelected"
                    OnRowDeselected="RadGrid1_RowDeselected" OnGridCreated="GridCreated" />
            </ClientSettings>
            <MasterTableView DataSourceID="SqlDataSource1" AutoGenerateColumns="False"
                DataKeyNames="OrderID" ClientDataKeyNames="OrderID">
                <CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
                <RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
                    <HeaderStyle Width="20px"></HeaderStyle>
                </RowIndicatorColumn>
                <ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
                    <HeaderStyle Width="20px"></HeaderStyle>
                </ExpandCollapseColumn>
                <Columns>
                    <telerik:GridClientSelectColumn UniqueName="SelectColumnName">
                    </telerik:GridClientSelectColumn>
                    <telerik:GridBoundColumn DataField="OrderID" DataType="System.Int32"
                        FilterControlAltText="Filter OrderID column" HeaderText="OrderID"
                        ReadOnly="True" SortExpression="OrderID" UniqueName="OrderID">
                    </telerik:GridBoundColumn>
                    <telerik:GridDateTimeColumn DataField="OrderDate" DataType="System.DateTime"
                        FilterControlAltText="Filter OrderDate column" HeaderText="OrderDate"
                        SortExpression="OrderDate" UniqueName="OrderDate">
                    </telerik:GridDateTimeColumn>
                    <telerik:GridNumericColumn DataField="Freight" DataType="System.Decimal"
                        FilterControlAltText="Filter Freight column" HeaderText="Freight"
                        SortExpression="Freight" UniqueName="Freight">
                    </telerik:GridNumericColumn>
                    <telerik:GridBoundColumn DataField="ShipName"
                        FilterControlAltText="Filter ShipName column" HeaderText="ShipName"
                        SortExpression="ShipName" UniqueName="ShipName" EditFormHeaderTextFormat="">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="ShipCountry"
                        FilterControlAltText="Filter ShipCountry column" HeaderText="ShipCountry"
                        SortExpression="ShipCountry" UniqueName="ShipCountry">
                    </telerik:GridBoundColumn>
                </Columns>
                <EditFormSettings>
                    <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                    </EditColumn>
                </EditFormSettings>
            </MasterTableView>
            <FilterMenu EnableImageSprites="False">
            </FilterMenu>
        </telerik:RadGrid>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
            SelectCommand="SELECT [OrderID], [OrderDate], [Freight], [ShipName], [ShipCountry] FROM [Orders]"></asp:SqlDataSource>
    </form>
</body>
</html>
