datalist分页(codebehind部分)

博客涉及ASP.NET相关内容,包含对象、搜索、命令行操作及字符串处理等信息技术方面的内容,但具体细节因内容缺失暂不明确。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

datalist分页(codebehind部分)

转贴:Andy.m   日期:2003-07-12   人气:30
using System;
using System.Collections;
using System.Configuration;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace cj168.Web
{
    /// <summary>
    /// Summary description for search.
    /// </summary>
    public class search : System.Web.UI.Page

    {
        protected System.Web.UI.WebControls.DataList datalistSearchResult;
        protected System.Web.UI.WebControls.Label lblTxtSearch;
        protected System.Web.UI.WebControls.Label lblRecordCount;
        protected System.Web.UI.WebControls.Label lblCurrentPage;
        protected System.Web.UI.WebControls.Label lblPageCount;
        protected System.Web.UI.WebControls.Label lblPageRange;

        protected System.Web.UI.WebControls.LinkButton lbnPrevPage;
        protected System.Web.UI.WebControls.LinkButton lbnNextPage;
        String txtSearch;
        int PageSize,RecordCount,PageCount,CurrentPage;
        

        public search()
        {
            Page.Init += new System.EventHandler(Page_Init);
        }

        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
            txtSearch = Request.Params["txtSearch"];
            if (txtSearch != null )
            {txtSearch = txtSearch.Replace("'","");
            
            lblTxtSearch.Text = txtSearch;

            //设定PageSize
            PageSize = 10;

            //计算总共有多少记录
            RecordCount = CalculateRecord();
            lblRecordCount.Text = RecordCount.ToString();

            if(!Page.IsPostBack)

                {
                

                DoSearch();
                CurrentPage = 0;
                ViewState["PageIndex"] = 0;

                

                //计算总共有多少页
                PageCount = RecordCount/PageSize;
                if (RecordCount%PageSize > 0)
                    PageCount = PageCount + 1;
                lblPageCount.Text = PageCount.ToString();
                ViewState["PageCount"] = PageCount;
                
                   
                }
            }

            


        }

        //计算总共有多少条记录
        public int CalculateRecord()
        {
            cj168.DataAccess.MagsDB DoSearch = new cj168.DataAccess.MagsDB();                            
            return DoSearch.GetSearchResultCount(txtSearch);            
            
        }



        void DoSearch()
        {                
            int StartIndex;
            int fromItem, toItem;
    
            //设定导入的起终地址
            StartIndex    = CurrentPage*PageSize;

            cj168.DataAccess.MagsDB DoSearch = new cj168.DataAccess.MagsDB();                            
            datalistSearchResult.DataSource = DoSearch.GetSearchResult(txtSearch,StartIndex, PageSize);            
            datalistSearchResult.DataBind();

            lbnNextPage.Enabled = true;
            lbnPrevPage.Enabled = true;
            if(CurrentPage==(PageCount-1)) lbnNextPage.Enabled = false;
            if(CurrentPage==0) lbnPrevPage.Enabled = false;
            lblCurrentPage.Text = (CurrentPage+1).ToString();

            fromItem = StartIndex+1;
            toItem = StartIndex+PageSize;

            if (RecordCount < toItem)
                toItem = RecordCount;

            lblPageRange.Text = fromItem + "-" + toItem;

        }


        

        public void Page_OnClick(Object sender,CommandEventArgs e)
        {
            CurrentPage = (int)ViewState["PageIndex"];
            PageCount    = (int)ViewState["PageCount"];

            string cmd = e.CommandName;
            //判断cmd,以判定翻页方向
            switch(cmd)
            {
                case "next":
                    if(CurrentPage<(PageCount-1)) CurrentPage++;
                    break;
                case "prev":
                    if(CurrentPage>0) CurrentPage--;
                    break;
            }

            ViewState["PageIndex"] = CurrentPage;

            DoSearch();
    
    
        }

        
        
        private void Page_Init(object sender, EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
        }

        #region Web Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {    
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值