惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
S
Securelist
博客园 - Franky
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
IT之家
IT之家
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
The Cloudflare Blog
云风的 BLOG
云风的 BLOG
N
News and Events Feed by Topic
AI
AI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Schneier on Security
Schneier on Security
Attack and Defense Labs
Attack and Defense Labs
Vercel News
Vercel News
腾讯CDC
Google DeepMind News
Google DeepMind News
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
M
MIT News - Artificial intelligence
WordPress大学
WordPress大学
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
Netflix TechBlog - Medium
量子位
S
Schneier on Security
Hacker News: Ask HN
Hacker News: Ask HN
Cyberwarzone
Cyberwarzone
S
Security Affairs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
N
News and Events Feed by Topic
T
Tenable Blog
PCI Perspectives
PCI Perspectives
MyScale Blog
MyScale Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cyber Attacks, Cyber Crime and Cyber Security
W
WeLiveSecurity
N
News | PayPal Newsroom
P
Proofpoint News Feed
O
OpenAI News
C
CERT Recently Published Vulnerability Notes
B
Blog
Cisco Talos Blog
Cisco Talos Blog
Microsoft Security Blog
Microsoft Security Blog
V
Visual Studio Blog
MongoDB | Blog
MongoDB | Blog
大猫的无限游戏
大猫的无限游戏
A
Arctic Wolf
Y
Y Combinator Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Spread Privacy
Spread Privacy

博客园 - kenly33

ORM及代码生成器和插件C#源码(最新版Kenly.DBFramework4.5.8.5) 處理RadioButtonList客戶端事件 - kenly33 - 博客园 对象关系映射架构(DBFramework)及代码生成器和插件(源码) 使對象支持自定義比較 統計分數 讀寫XML文檔 - kenly33 - 博客园 Visual Studio Team System简介 过滤某些字段重复的记录 MultiFileUploador Gridview梆定 兩個輸入框只允許一個輸入 Gridview中梆定得RadioButton Javascript 隱藏或顯示某區域 ASP.NET 2.0中合并 GridView 的表头单元格 VS2005 RESOURCE MANAGEMENT 鲸鱼哲学 Javascript彈出對話框 FxCop Rules Atlas使用演练
GridViewNavigator
kenly33 · 2007-05-07 · via 博客园 - kenly33

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="GridViewNavigator.ascx.cs"
    Inherits="Controls_GridViewNavigator" %>
<link href="../Css/ToolBar.css" type="text/css" rel="stylesheet" />
<div id="tblNoData" runat="server" style="border-top:solid 1px #EEE;border-bottom:solid 1px #EEE; padding: 10px 0px;
    background-color: #f9f9f9; text-align: center;">
    <asp:Label ID="lblNoData" runat="server" Text="當前資料庫中沒有符合條件的資料。" ForeColor="#FF7E42"></asp:Label>
</div>
<div id="tblPage" runat="server" style="text-align: right; padding-right: 10px;">
    <table width="100%">
        <tr>
            <td style="width: 50%" align=left>
                <asp:Label ID="lblItemNumber" runat="server" Enabled="False">總條目數</asp:Label>:
                <asp:Label ID="lblRecordCount" ForeColor="Red" runat="server" Enabled="False"></asp:Label>&nbsp;
                <asp:Label ID="lblPageItemNumber" runat="server" Enabled="False">每頁條目數</asp:Label>:
                <asp:Label ID="lblPageSize" ForeColor="Red" runat="server" Enabled="False"></asp:Label>&nbsp;
            </td>
            <td style="width: 50%" align=right>
               <asp:Label ID="lblCurrentPage" runat="server" Enabled="False">頁碼</asp:Label>:
                <asp:Label ID="lblPageIndex" ForeColor="Red" runat="server" Enabled="False"></asp:Label>
                <asp:Label ID="lblSplit" runat="server" Text="Label" Enabled="false">/</asp:Label>
                <asp:Label ID="lblPageCount" ForeColor="Red" runat="server" Enabled="False"></asp:Label>&nbsp;
               
                <asp:LinkButton ID="lnkFirst" runat="server" CausesValidation="False" CommandName="First"
                    Enabled="False">首頁</asp:LinkButton>&nbsp;
                <asp:LinkButton ID="lnkPrevious" runat="server" CausesValidation="False" CommandName="Previous"
                    Enabled="False">上一頁</asp:LinkButton>&nbsp;
                <asp:LinkButton ID="lnkNext" runat="server" CausesValidation="False" CommandName="Next"
                    Enabled="False">下一頁</asp:LinkButton>&nbsp;
                <asp:LinkButton ID="lnkLast" runat="server" CausesValidation="False" CommandName="Last"
                    Enabled="False">尾頁</asp:LinkButton>&nbsp;
                <asp:TextBox ID="txtPage" runat="server" CssClass="txtPage" Width="30px" Wrap="False"
                    Enabled="False">1</asp:TextBox>&nbsp;
                <asp:Button ID="btnGo" runat="server" CssClass="btnGo" Enabled="False"></asp:Button>
            </td>
        </tr>
    </table>

  
   
   
</div>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Threading;

public partial class Controls_GridViewNavigator : System.Web.UI.UserControl
{
    private string _gridViewId = "";
  
    private int _pageIndex = 1;
    private int _pageSize = 25;
    private string _keyField = "";
    //private string _sortField = "";
    //private SortType _sortType = SortType.ASC;

    private DataTable _dataSource;

    public enum SortType
    {
        ASC,DESC
    }

    protected void Page_Load(object sender, System.EventArgs e)
    {
        if (!this.IsPostBack)
        {
            this.Unload += new EventHandler(Controls_GridViewPage_Unload);
            txtPage.Attributes.Add("onkeypress", "if(event.keyCode == 13){document.all." + btnGo.ClientID + ".click();event.returnValue=false;}");
            txtPage.Attributes.Add("onmouseover", "if(!document.all." + txtPage.ClientID + ".style.disabled){document.all." +
                txtPage.ClientID + ".focus();document.all." + txtPage.ClientID + ".select();}");
        }
    }

    void Controls_GridViewPage_Unload(object sender, EventArgs e)
    {
        if (Session["GridViewDataSource"] != null)
        {
            Session["GridViewDataSource"] = null;
        }
    }

    #region Properties

    /// <summary>
    /// 顯示資料表的DataGrid名稱
    /// </summary>
    public string GridViewId
    {
        get
        {
            return _gridViewId;
        }
        set
        {
            this._gridViewId = value;
        }
    }

    /// <summary>
    /// 取得用於確定DataGrid每一行的字段名稱,如:RoleID
    /// </summary>
    public string KeyField
    {
        get
        {
            return _keyField;
        }
        set
        {
            _keyField = value;
        }
    }

    /// <summary>
    /// 排序字段
    /// </summary>
    //public string SortField
    //{
    //    get
    //    {
    //        return _sortField;
    //    }
    //    set
    //    {
    //        _sortField = value;
    //    }
    //}

    /// <summary>
    /// 排序方式
    /// </summary>
    //public SortType SortDirection
    //{
    //    get
    //    {
    //        return _sortType;
    //    }
    //    set
    //    {
    //        _sortType = value;
    //    }
    //}

    /// <summary>
    /// 設定或取得當前頁碼
    /// </summary>
    public int PageIndex
    {
        get
        {
            //return _pageIndex;
            return (ViewState["PageIndex"] == null) ? _pageIndex : int.Parse(this.ViewState["PageIndex"].ToString());
        }
        set
        {
            _pageIndex = value;
            if (this.ViewState["PageIndex"] == null)
            {
                this.ViewState.Add("PageIndex", value);
            }
            else
            {
                this.ViewState["PageIndex"] = value;
            }
        }
    }

    ///<summary>
    ///設置pageSize,每頁顯示的紀錄條數
    ///</summary>
    public int PageSize
    {
        get
        {
            return _pageSize;
        }
        set
        {
            _pageSize = value;
        }
    }

    /// <summary>
    /// 用于綁定數據的Table
    /// </summary>
    public DataTable DataSource
    {
        get
        {
            if (Session["GridViewDataSource"] != null)
            {
                this._dataSource = Session["GridViewDataSource"] as System.Data.DataTable;
            }
            return this._dataSource;
        }
        set
        {
            this._dataSource = value;
            if (Session["GridViewDataSource"] == null)
            {
                Session.Add("GridViewDataSource", this._dataSource);
            }
            else
            {
                Session["GridViewDataSource"] = this._dataSource;
            }
            Navigate();
        }
    }

    #endregion

    /// <summary>
    /// 導向到某一頁面
    /// </summary>
    /// <param name="pageIndex">頁碼</param>
    public void Navigate(int targetPageIndex)
    {
        // 取得總頁數
        int pageCount=GetPageCount();

        if (targetPageIndex < 1)
        {
            PageIndex = 1;
        }
        if (targetPageIndex > pageCount)
        {
            PageIndex = pageCount;
        }
        else
        {
            PageIndex = targetPageIndex;
        }

        ShowPage();
    }

    /// <summary>
    /// 初始化資料,並顯示第一頁
    /// </summary>
    private void Navigate()
    {
        PageIndex = 1;
        ShowPage();
    }

    /// <summary>
    /// 綁定數據
    /// </summary>
    private void ShowPage()
    {
        try
        {
            BindGridViewData();
            DisplayPagingStatus();
            EnablePagingControls();
        }
        catch (Exception ex)
        {
            string msg = ex.Message;
            throw ex;
        }
    }

    private void BindGridViewData()
    {
        try
        {
            System.Web.UI.Control ctrl = this.Parent;
            GridView GridViewControl = (GridView)ctrl.FindControl(_gridViewId);
            if (this._pageSize > 0)
            {
                GridViewControl.PageSize = this._pageSize;
            }

            GridViewControl.DataSource = this.DataSource;
            if (!string.IsNullOrEmpty(this._keyField))
            {
                GridViewControl.DataKeyNames = new string[] { _keyField };
            }
            GridViewControl.PageIndex = this.PageIndex - 1;
            GridViewControl.DataBind();

            if (DataSource.Rows.Count <= 0)
            {
                tblNoData.Visible = true;
                tblPage.Visible = false;
            }
            else
            {
                tblNoData.Visible = false;
                tblPage.Visible = true;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private void DisplayPagingStatus()
    {
        int pageCount = this.GetPageCount();
        int recordCount = this.GetRecordCount();

        lblPageIndex.Text = PageIndex.ToString();
        lblPageCount.Text = pageCount.ToString();
        lblRecordCount.Text = recordCount.ToString();
        lblPageSize.Text = _pageSize.ToString();
        txtPage.Text = PageIndex.ToString();
    }

 
    /// <summary>
    /// 設置翻頁按鈕的狀態
    /// </summary>
    public void EnablePagingControls()
    {
        int pageCount = this.GetPageCount();

        if (pageCount == 0)
        {
            lblCurrentPage.Enabled = false;
            lblPageIndex.Enabled = false;
            lblSplit.Enabled = false;
            lblPageCount.Enabled = false;
            lblItemNumber.Enabled = false;
            lblRecordCount.Enabled = false;
            lblPageItemNumber.Enabled = false;
            lblPageSize.Enabled = false;
        }
        else
        {
            lblCurrentPage.Enabled = true;
            lblPageIndex.Enabled = true;
            lblSplit.Enabled = true;
            lblPageCount.Enabled = true;
            lblItemNumber.Enabled = true;
            lblRecordCount.Enabled = true;
            lblPageItemNumber.Enabled = true;
            lblPageSize.Enabled = true;

        if (pageCount <= 1)
        {   //小於等於一頁
            this.lnkFirst.Enabled = false;
            this.lnkPrevious.Enabled = false;
            this.lnkNext.Enabled = false;
            this.lnkLast.Enabled = false;
            this.txtPage.Enabled = false;
            this.btnGo.Enabled = false;
        }
        else
        {
            //有多頁
            if (PageIndex == 1)
            {
                //當前為第一頁
                this.lnkFirst.Enabled = false;
                this.lnkPrevious.Enabled = false;
                this.lnkNext.Enabled = true;
                this.lnkLast.Enabled = true;
            }
            else if (PageIndex == pageCount)
            {
                //當前為最後頁
                this.lnkFirst.Enabled = true;
                this.lnkPrevious.Enabled = true;
                this.lnkNext.Enabled = false;
                this.lnkLast.Enabled = false;
            }
            else
            {  //中間頁
                this.lnkFirst.Enabled = true;
                this.lnkPrevious.Enabled = true;
                this.lnkNext.Enabled = true;
                this.lnkLast.Enabled = true;
            }
            this.txtPage.Enabled = true;
            this.btnGo.Enabled = true;
        }
        }
    }

    /// <summary>
    /// 取得總頁數
    /// </summary>
    private int GetPageCount()
    {
        int recordCount = GetRecordCount();
        int pageCount = (recordCount % _pageSize == 0) ? Convert.ToInt32(recordCount / _pageSize) : Convert.ToInt32(recordCount / _pageSize) + 1;
        return pageCount;
    }

    /// <summary>
    /// 取得總條目數
    /// </summary>
    /// <returns></returns>
    private int GetRecordCount()
    {
        return this.DataSource.Rows.Count;
    }

    #region EventHandler

    /// <summary>
    /// 頁碼變換:上頁、下頁、前頁、后頁
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void OnNavigate(object sender, System.Web.UI.WebControls.CommandEventArgs e)
    {
        LinkButton lnkPage = (LinkButton)sender;
    
        this.PageIndex=CalculateTargetPageIndex(lnkPage.CommandName);
        ShowPage();
    }

    /// <summary>
    /// 直接跳轉到某一頁
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnGo_Click(object sender, System.EventArgs e)
    {
        try
        {
            int inputPageIndex = 0;
            int.TryParse(txtPage.Text.Trim(), out inputPageIndex);
            this.PageIndex = CalculateTargetPageIndex(inputPageIndex);
            ShowPage();
        }
        catch (Exception ex)
        {
            string msg = ex.Message;
            throw ex;
        }
    }

    private int CalculateTargetPageIndex(string commandName)
    {
        int targetPageIndex = 0;
        int pageCount = this.GetPageCount();

        switch (commandName)
        {
            case "First": // 第1頁
                {
                    targetPageIndex = 1;
                }
                break;

            case "Previous": // 前頁
                if (PageIndex > 1)
                {
                    targetPageIndex = PageIndex - 1;
                }
                break;
            case "Next": // 后頁
                if (PageIndex < pageCount)
                {
                    targetPageIndex = PageIndex + 1;
                }
                break;
            case "Last": // 尾頁
                {
                    targetPageIndex = pageCount;
                }
                break;
        }

        return targetPageIndex;

    }

    private int CalculateTargetPageIndex(int intputPageIndex)
    {
        int targetPageIndex = 0;
        int pageCount = this.GetPageCount();

        if (intputPageIndex > pageCount)
        {
            targetPageIndex = pageCount;
        }
        else if (intputPageIndex <= 1)
        {
            targetPageIndex = 1;
        }
        else
        {
            targetPageIndex = intputPageIndex;
        }

        return targetPageIndex;
    }

    #endregion

    #region Web Form 設計工具產生的程式碼

    override protected void OnInit(EventArgs e)
    {
        //
        // CODEGEN: 此為 ASP.NET Web Form 設計工具所需的呼叫。
        //
        InitializeComponent();
        base.OnInit(e);
    }

    /// <summary>
    ///        此為設計工具支援所必須的方法 - 請勿使用程式碼編輯器修改
    ///        這個方法的內容。
    /// </summary>
    private void InitializeComponent()
    {
        this.lnkFirst.Command += new System.Web.UI.WebControls.CommandEventHandler(OnNavigate);
        this.lnkPrevious.Command += new System.Web.UI.WebControls.CommandEventHandler(OnNavigate);
        this.lnkNext.Command += new System.Web.UI.WebControls.CommandEventHandler(OnNavigate);
        this.lnkLast.Command += new System.Web.UI.WebControls.CommandEventHandler(OnNavigate);
        this.btnGo.Click += new System.EventHandler(this.btnGo_Click);
        this.Load += new System.EventHandler(this.Page_Load);

    }

    #endregion
}