<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="GridView_Pagination._Default" %>

<%@ Register Assembly="C1.Web.Wijmo.Controls.48" Namespace="C1.Web.Wijmo.Controls.C1ReportViewer" TagPrefix="wijmo" %>
<%@ Register Assembly="C1.Web.Wijmo.Controls.48" Namespace="C1.Web.Wijmo.Controls.C1Dialog" TagPrefix="wijmo" %>
<%@ Register Assembly="C1.Web.Wijmo.Controls.48" Namespace="C1.Web.Wijmo.Controls.C1GridView" TagPrefix="wijmo" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">

    <asp:UpdatePanel ID="UpdatePanelGrid" runat="server" UpdateMode="Conditional">
        <ContentTemplate>

            <%-- Hidden fields MUST be inside the UpdatePanel so their values are
                 refreshed on every partial postback (paging, toggle click). --%>
            <asp:HiddenField ID="HSelectionAll" runat="server" />
            <asp:HiddenField ID="HSelected"     runat="server" />
            <asp:HiddenField ID="HUnselected"   runat="server" />
            <asp:HiddenField ID="HAllItems"     runat="server" />

            <wijmo:C1GridView ID="C1GridView1" runat="server"
                AutogenerateColumns="false"
                AllowPaging="true"
                PageSize="10"
                DataKeyNames="ID"
                OnPageIndexChanging="C1GridView1_PageIndexChanging"
                OnRowDataBound="C1GridView1_RowDataBound" 
                OnRowCommand="C1GridView1_RowCommand">
            </wijmo:C1GridView>


            <wijmo:C1Dialog ID="ReportDialog" runat="server"
                Title="Report Viewer"
                Width="900px" 
                Height="800px"
                ShowOnLoad="false"
                Modal="true"
                Resizable="true">
                <Content>
                    <wijmo:C1ReportViewer ID="ReportViewer" runat="server"
                        Width="860px"
                        Height="650px"
                        Zoom="100%"
                        CollapseToolsPanel="false"
                        EnableLogs="false" />
                </Content>
            </wijmo:C1Dialog>

            <script type="text/javascript">
                // Called from each row checkbox onclick – updates hidden fields
                // client-side instantly without triggering a postback.
                var ChangeSelection = function (oCheck, id) {
                    console.log('ChangeSelection: ' + id + ' checked=' + oCheck.checked);
                    try {
                        var hSel = document.getElementById('<%= HSelected.ClientID %>');
                        var hUnsel = document.getElementById('<%= HUnselected.ClientID %>');

                        var oSelected = hSel.value ? hSel.value.split(',').map(Number) : [];
                        var oUnselected = hUnsel.value ? hUnsel.value.split(',').map(Number) : [];

                        if (oCheck.checked) {
                            var iU = oUnselected.indexOf(id);
                            if (iU >= 0) oUnselected.splice(iU, 1);
                            if (oSelected.indexOf(id) < 0) oSelected.push(id);
                        } else {
                            var iS = oSelected.indexOf(id);
                            if (iS >= 0) oSelected.splice(iS, 1);
                            if (oUnselected.indexOf(id) < 0) oUnselected.push(id);
                        }

                        hSel.value = oSelected.join(',');
                        hUnsel.value = oUnselected.join(',');
                    } catch (ex) {
                        alert('ChangeSelection: ' + ex);
                    }
                };
            </script>

        </ContentTemplate>
    </asp:UpdatePanel>

</asp:Content>
