NewsRhzhi | 先创资讯 | 旧版入口
rhzhi.net
网站首页 | NewsRhzhi | 先创资讯 | 操作系统 | 工具软件 | 办公软件 | 网站设计 | 组网专栏 | 平面设计 | 多 媒 体 | 程序开发 | 硬件资料 | 聊天软件
您现在的位置: 先创网 >> 程序开发 >> .NET >> 文章正文
datalist分页(codebehind部分)
不详
2005-3-14文/佚名
    

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
    }
}

打印此页 投稿与建议 返回顶部
栏 目 索 引
软件应用 SOFTWARE
Win XP | NT/2003
Win2000 | DOS/Win9x
PowerPoint | Office
Excel | Word
网络软件 | 实用软件
媒体软件 | 系统软件
常用软件 | 办公软件
聊天软件 | 网络安全
新软试用 | Vista
设计在线 DESIGN
Dreamweaver | 3DMax
Photoshop | Flash
平面设计 | 网页设计
多 媒 体 | 精品画廊
精彩专区 SPECIAL
Q Q 专区 | 热门专题
组网玩网 | 程序开发
应用集锦 |

没有任何图片文章
相关文章
关于我们 - 联系方式 - 合作伙伴 - 网站大事记 - 网站地图 - 我要投稿
Copyright ©1997-2008 先创网 All Rights Reserved.
先创科技 版权所有