<%BeginASP%>@ Control Src="fpdbnet.cs" Language="c#" Inherits="Microsoft.Office.FrontPage.DBInterface.DBRegionBase" <%EndASP%>
<%BeginASP%>@ Import Namespace="System.Collections" <%EndASP%>
<%BeginASP%>@ Import Namespace="System.Data" <%EndASP%>
<%BeginASP%>@ Import Namespace="System.Globalization" <%EndASP%>
<%BeginASP%>@ Register TagPrefix="FPDB" TagName="DBQuery" Src="DBQuery.ascx" <%EndASP%>
<SCRIPT Language="C#" Runat="server">
        Microsoft.Office.FrontPage.DBInterface.DBConnection _connection;
        private DataGrid targetGrid;
        private DBQuery userFieldsControl;
        private Label noResultsLabel;

        private bool bPage;
        private int nPageSize;
        private string _customSql;
        private string _dBName;
        private string _maxRecords;
        private string _noResults;
        private string _orderBy;
        private string _pageSize;
        private string _rootPath;
        private string _tableKey;
        private string _tableName;
        private string _target;
        private string _usePaging;
        private string _userFields;
        private string _where;



        public string AllowPaging
        {
            get
            {
                return _usePaging;
            }
            set
            {
                _usePaging = value;
            }
        }

        public string CustomSql
        {
            get
            {
                return _customSql;
            }
            set
            {
                _customSql = value;
            }
        }
    
        public string DBName
        {
            get
            {
                if(_dBName == null)
                    _dBName = System.Configuration.ConfigurationSettings.AppSettings["DBName"];
                return _dBName;
            }
            set
            {
                _dBName = value;
            }
        }

        public string MaxRecords
        {
            get
            {
                return _maxRecords;
            }
            set
            {
                _maxRecords = value;
            }


        }

        public string NoResults
        {
            get
            {
                return _noResults;
            }
            set
            {
                _noResults = value;
            }
        }
    
        public string OrderBy
        {
            get
            {
                return _orderBy;
            }
            set
            {
                _orderBy = value;
            }
        }
        
        public string PageSize
        {
            get
            {
                return _pageSize;
            }
            set
            {
                _pageSize = value;
            }
        }

        public string RootPath
        {
            get
            {
                if(_rootPath == null)
                    _rootPath = ConfigurationSettings.AppSettings["RootPath"];
                return _rootPath;
            }
            set
            {
                _rootPath = value;
            }
        }

        
        
        public string TableKey
        {
            get
            {
                if(_tableKey == null)
                    _tableKey = ConfigurationSettings.AppSettings["TableKey"];
                return _tableKey;
            }
            set
            {
                _tableKey = value;
            }
        }

        public string TableName
        {
            get
            {
                if(_tableName == null )
                    _tableName = System.Configuration.ConfigurationSettings.AppSettings["TableName"];
                if(_tableName == null || _tableName.Length == 0)
                    _tableName = "Table";
                return _tableName;
            }
            set
            {
                _tableName = value;
            }
        }
        
        public string Target
        {
            get
            {
                return _target;
            }
            set
            {
                _target = value;
            }
        }
        
        public string UserFields
        {
            get
            {
                return _userFields;
            }
            set
            {
                _userFields = value;
            }
        }
        public string Where
        {
            get
            {
                return _where;
            }
            set
            {
                _where = value;
            }
        }
            
        public override void SetConnection( Microsoft.Office.FrontPage.DBInterface.DBConnection connection )
        {
            _connection = connection;
            ApplyConfigSettings();
        }
    
        protected override void OnInit( EventArgs e )
        {
            if(_connection == null)
            {
                _connection = new Microsoft.Office.FrontPage.DBInterface.DBConnection(Page);
            }
            if(Target != null)
            {
                System.Web.UI.Control theTargetCandidate = Page.FindControl(Target);
                if(theTargetCandidate is DataGrid)
                {
                    targetGrid = (DataGrid)theTargetCandidate;
                }
            }
            if(NoResults != null)
            {
                System.Web.UI.Control NoResultsCandidate = Page.FindControl(NoResults);
                if(NoResultsCandidate is Label)
                {
                    noResultsLabel = (Label)NoResultsCandidate;
                }
            }
            if(UserFields != null)
            {
                System.Web.UI.Control UserFieldsCandidate = Page.FindControl(UserFields);
                if(UserFieldsCandidate is DBQuery)
                {
                    userFieldsControl = (DBQuery)UserFieldsCandidate;
                }
            }
        
            if( AllowPaging != null )
                bPage = AllowPaging.ToLower(CultureInfo.InvariantCulture).Equals("true");
            
            try{
                if( PageSize != null )
                {
                    nPageSize = Convert.ToInt32( PageSize, CultureInfo.InvariantCulture.NumberFormat );
                }
                else
                {
                    nPageSize = 5;
                }
            }
            catch( Exception ){
                bPage = false;
            }
            ApplyConfigSettings();
        
            if( bPage )
            {
                pagerPanel.Visible = true;
                targetGrid.AllowPaging = true;
                targetGrid.PagerStyle.Visible = false;
                targetGrid.PageSize = nPageSize;
                if ( Session["DataRowAdded"] != null && Session["DataRowAdded"].Equals("true"))
                {
                    UpdatePager(true);
                    Session["DataRowAdded"] = null;
                }
                else
                {
                    UpdatePager(false);
                }
            
            }
        }

        protected override void OnPreRender( EventArgs e )
        {
            if(!Page.IsPostBack || ( userFieldsControl != null && userFieldsControl.Submitted ))
            {
                DataBind();
            }
        
            UpdatePager(false);
        }
    
        public override void DataBind()
        {
            if(targetGrid == null)
            {
                return;
            }
        
            UpdatePager(false);
            DataView theDataView = GetDataView();
            if(noResultsLabel != null)
            {
                if(theDataView.Count == 0)
                    noResultsLabel.Visible = true;
                else
                    noResultsLabel.Visible = false;
            }

        
            targetGrid.DataSource = theDataView;
        
            targetGrid.DataBind();
        }
    
        private void ApplyConfigSettings()
        {
            _connection.DBName = DBName;
            _connection.RootPath = RootPath;
            _connection.TableKey = TableKey;
            _connection.TableName = TableName;
            _connection.MaxRecordCount = Convert.ToInt32( _maxRecords, CultureInfo.InvariantCulture.NumberFormat );
            if( CustomSql != null )
            {
                _connection.CacheName = Target;
            }
            else
            {
                _connection.CacheName = DBName + TableName;
            }

            if(CustomSql != null)
            {
                _connection.SearchQuery = CustomSql;
            } 
        }

        private DataView GetDataView()
        {
            string fpQuery = "";
            if( Where != null )
            {
                fpQuery = DBQuery.CompleteWhereClause(userFieldsControl, Where );            
            }


            if((CustomSql != null))
            {
                ArrayList paramList = new ArrayList();
                _connection.SearchQuery = DBQuery.CompleteCustomSql( CustomSql, userFieldsControl, paramList );              
                _connection.SetConnectionParameters(paramList);
            }
	    if((userFieldsControl != null && userFieldsControl.Submitted) || !Page.IsPostBack)
            {
                _connection.RefreshCache();
            }
            return _connection.GetDataView( fpQuery, OrderBy );
        }
    
        private void UpdatePager(bool useLastPage)
        {
            if(targetGrid == null)
            {
                return;
            }

            int VirtualItemCount = GetDataView().Count;
        
            int nMaxPage = ( VirtualItemCount - 1 ) / nPageSize;
        
            if( targetGrid.CurrentPageIndex < 0 )
                {targetGrid.CurrentPageIndex = 0;}
            if( targetGrid.CurrentPageIndex > nMaxPage || useLastPage)
                {targetGrid.CurrentPageIndex = nMaxPage;}
            
            buttonFirst.Enabled = buttonPrevious.Enabled = ( targetGrid.CurrentPageIndex != 0 );
            buttonLast.Enabled = buttonNext.Enabled = ( targetGrid.CurrentPageIndex != nMaxPage );
        }
    
        protected void CommandChangePage( Object s, CommandEventArgs e )
        {
            bool lastPage = false;
            if( e.CommandName == "First" )
                {targetGrid.CurrentPageIndex  = 0;}
            else if( e.CommandName == "Prev" )
                {targetGrid.CurrentPageIndex --;}
            else if( e.CommandName == "Next" )
                {targetGrid.CurrentPageIndex ++;}
            else if( e.CommandName == "Last" )
                {lastPage = true;}
            
            UpdatePager(lastPage);
            DataBind();
        }
</SCRIPT>
<asp:Panel ID="pagerPanel" Runat="server" Visible="False">
    <asp:Button id="buttonFirst" Runat="server" Text=" |&lt; " CausesValidation="False" OnCommand="CommandChangePage" CommandName="First"></asp:Button>
    <asp:Button id="buttonPrevious" Runat="server" Text="  &lt; " CausesValidation="False" OnCommand="CommandChangePage" CommandName="Prev"></asp:Button>
    <asp:Button id="buttonNext" Runat="server" Text=" &gt;  " CausesValidation="False" OnCommand="CommandChangePage" CommandName="Next"></asp:Button>
    <asp:Button id="buttonLast" Runat="server" Text=" &gt;| " CausesValidation="False" OnCommand="CommandChangePage" CommandName="Last"></asp:Button>
</asp:Panel>
