Web用户控件 分页

前台:

<style type="text/css">
    .button
        {
            color: #135294;
            border: 1px solid #666;
            height: 21px;
            line-height: 18px;
            background: url("/Images/AdminMainImg/button_bg.gif");
        }
    .label{ color:Red;}
</style>
<div>
    共<asp:Label ID="lblPageCount" Text="" runat="server" CssClass="label" />页,
    当前为第<asp:Label ID="lblCurrentPage" Text="" runat="server" CssClass="label" />页,
    每页<asp:Label ID="lblPageSize" Text="3" runat="server" CssClass="label" />条数据<br /><br />
    <asp:Button ID="btnFirstPage" runat="server" Text="首页" CssClass="button"
        οnclick="btnFirstPage_Click" />&nbsp;&nbsp;
    <asp:Button ID="btnPrePage" runat="server" Text="上一页" CssClass="button"
        οnclick="btnPrePage_Click" />&nbsp;&nbsp;
    <asp:Button ID="btnNextPage" runat="server" Text="下一页" CssClass="button"
        οnclick="btnNextPage_Click" />&nbsp;&nbsp;
    <asp:Button ID="btnEndPage" runat="server" Text="尾页" CssClass="button"
        οnclick="btnEndPage_Click"/>&nbsp;&nbsp;
    <asp:Label ID="Label2" runat="server" Text="跳转到"></asp:Label >
    <asp:TextBox ID="txtPage" runat="server" Width="30px"></asp:TextBox>&nbsp;&nbsp;
    <asp:Button ID="btnGo" runat="server" Text="Go" CssClass="button"
        οnclick="btnGo_Click" />&nbsp;&nbsp;
        <asp:Label ID="lblSign" runat="server" Text="" style=" color:Red"></asp:Label>
</div>

后台cs代码:

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

using BookShopOnLine.Model;
using BookShopOnLine.BLL;

namespace BookShopOnLine.Web.Admin
{
    public partial class Paging : System.Web.UI.UserControl
    {
        BookShop_BookTypeBLL _BookTypeBLL = new BookShop_BookTypeBLL();
        public List<BookShop_BookTypeModel> list = new List<BookShop_BookTypeModel>();

        private string _tableName; //表的名称
        private Control _dataControl; //数据绑定控件
        private string _sort; //排序
        private string _column; //要排序的字段

        #region 属性
        public string TableName
        {
            get { return _tableName; }
            set { _tableName = value; }
        }

        public Control DataControl
        {
            get { return _dataControl; }
            set { _dataControl = value; }
        }

        public string Sort
        {
            get { return _sort; }
            set { _sort = value; }
        }

        public string Column
        {
            get { return _column; }
            set { _column = value; }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int recordCount = _BookTypeBLL.GetRecordCount("BookType_Delete=0"); //数据总条数
                ViewState["RecordCount"] = recordCount;
                int result = recordCount % 3; //取模运算,得到余数
                this.lblPageCount.Text = (recordCount / 3 + (result == 0 ? 0 : 1)).ToString(); //总页数
                ViewState["PageCount"] = this.lblPageCount.Text;

                LoadGridView();
            }
            int page = 0; //输入的要调整的页码
            if (this.txtPage.Text.Trim() == "")
            {
                page = 0;
            }
            else
            {
                page = int.Parse(this.txtPage.Text.Trim());
            }
            if (page > int.Parse(this.lblPageCount.Text))
            {
                LoadGridView();
            }
        }

        /// <summary>
        /// 得到控件类型
        /// </summary>
        private void GetControlType(List<BookShop_BookTypeModel> list)
        {
            if (_dataControl is GridView)
            {
                GridView gv = _dataControl as GridView;
                gv.DataSource = list;
                gv.DataBind();
            }
        }

        /// <summary>
        /// 首页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnFirstPage_Click(object sender, EventArgs e)
        {
            LoadGridView();
        }

        /// <summary>
        /// 首页加载数据
        /// </summary>
        private void LoadGridView()
        {
            this.btnPrePage.Enabled = false;
            list = _BookTypeBLL.GetModelListByProc(_tableName, _column, _sort, 1, int.Parse(this.lblPageSize.Text.Trim()));
            this.lblCurrentPage.Text = "1";
            ViewState["CurrentPage"] = int.Parse(this.lblCurrentPage.Text.Trim()) + 1;
            GetControlType(list);
        }

        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnPrePage_Click(object sender, EventArgs e)
        {
            this.btnNextPage.Enabled = true;
            if (int.Parse(this.lblCurrentPage.Text) == 2)
            {
                this.btnPrePage.Enabled = false; //上一页按钮不可以
            }
            int n = int.Parse(this.lblCurrentPage.Text.Trim());
            n--;
            this.lblCurrentPage.Text = n.ToString();
            list = _BookTypeBLL.GetModelListByProc(_tableName, _column, _sort, n, int.Parse(this.lblPageSize.Text.Trim()));
            GetControlType(list);
        }

        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnNextPage_Click(object sender, EventArgs e)
        {
            this.btnPrePage.Enabled = true;
            int n1 = int.Parse(this.lblCurrentPage.Text);
            int n2 = int.Parse(ViewState["PageCount"].ToString());
            if (n1 + 1 == n2)
            {
                this.btnNextPage.Enabled = false;
            }
            int n = int.Parse(this.lblCurrentPage.Text.Trim());
            n++;
            this.lblCurrentPage.Text = n.ToString();
            list = _BookTypeBLL.GetModelListByProc(_tableName, _column, _sort, n, int.Parse(this.lblPageSize.Text.Trim()));
            GetControlType(list);
        }

        /// <summary>
        /// 尾页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnEndPage_Click(object sender, EventArgs e)
        {
            this.btnNextPage.Enabled = false;
            this.btnPrePage.Enabled = true;
            list = _BookTypeBLL.GetModelListByProc(_tableName, _column, _sort, int.Parse(ViewState["PageCount"].ToString()), int.Parse(this.lblPageSize.Text.Trim()));
            this.lblCurrentPage.Text = ViewState["PageCount"].ToString();
            GetControlType(list);
        }

        /// <summary>
        /// 跳转页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnGo_Click(object sender, EventArgs e)
        {
            if (int.Parse(this.txtPage.Text.Trim()) > int.Parse(this.lblPageCount.Text))
            {
                this.lblSign.Text = "已超出最大页码!请重新输入。。。";
            }
            else
            {
                list = _BookTypeBLL.GetModelListByProc(_tableName, _column, _sort, int.Parse(this.txtPage.Text.Trim()), int.Parse(this.lblPageSize.Text.Trim()));
                this.lblCurrentPage.Text = this.txtPage.Text.Trim();
                GetControlType(list);
            }
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值