ASP.NET中DataList分页功能

转载 2006年06月07日 11:22:00

   CREATE procedure wqnews_GetPagedWQNews
(@pagesize int,
@pageindex int,
@docount bit)
as
set nocount on
if(@docount=1)
select count(ArticleID) from Article
else
begin
declare @indextable table(id int identity(1,1),nid int)
declare @PageLowerBound int
declare @PageUpperBound int
set @PageLowerBound=(@pageindex-1)*@pagesize
set @PageUpperBound=@PageLowerBound+@pagesize
set rowcount @PageUpperBound
insert into @indextable(nid) select ArticleID from Article
select O.ArticleID,O.Content,O.Title,O.PublishTime,O.UserName,O.UserLogo,O.IP,O.HomePage,O.Email,O.OICQ from Article O,@indextable t where O.ArticleID=t.nid
and t.id>@PageLowerBound and t.id<=@PageUpperBound order by t.id
end
set nocount off
GO
程序:
         SqlConnection conn;
        SqlCommand cmd;
        static int CurrentPageIndex;
        const int PageSize = 10;
        static int RecordCount;
        static int PageCount;

        private void Page_Load(object sender, System.EventArgs e)
        {
            DateTime CurrentTime = DateTime.Now;
            conn = new SqlConnection(ConfigurationSettings.AppSettings["SqlConnString"]);
            if(!Page.IsPostBack)
            {
                cmd  = new SqlCommand("wqnews_GetPagedWQNews", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@pageindex",1);
                cmd.Parameters.Add("@pagesize",1);
                cmd.Parameters.Add("@docount",true);
                conn.Open();
                RecordCount = (int)cmd.ExecuteScalar();
                int RemRecord;
                PageCount = Math.DivRem(RecordCount, PageSize, out RemRecord);
                if(RemRecord != 0)
                {
                    PageCount += 1;
                }
                conn.Close();
                BindData();
            }
            TimeSpan PageDisplayTimeSpan = DateTime.Now - CurrentTime;
            DisplayTimeSpan.Text = PageDisplayTimeSpan.Milliseconds.ToString();
        }

        private void BindData()
        {
            if(Request.QueryString["page"] == null)
            {
                CurrentPageIndex = 1;
            }
            else
            {
                CurrentPageIndex = Int32.Parse(Request.QueryString["page"]);
            }
            cmd=new SqlCommand("wqnews_GetPagedWQNews",conn);
            cmd.CommandType=CommandType.StoredProcedure;
            cmd.Parameters.Add("@pageindex",CurrentPageIndex);
            cmd.Parameters.Add("@pagesize",PageSize);
            cmd.Parameters.Add("@docount",false);
            conn.Open();
            DataList1.DataSource=cmd.ExecuteReader();
            DataList1.DataBind();
            conn.Close();

            GetRecordInfo();
            GetPageInfo();
        }

        private void GetRecordInfo()
        {
            //RecordInfo.Text = "记录总数:<font color=/"#0000FF/">" + RecordCount + "</font>总页数<font color=/"#0000FF/">" + PageCount + "</font>:当前页:<font color=/"0000FF/">" + CurrentPageIndex + "</font>";
            RecordInfo.Text = "页次:<strong> " + CurrentPageIndex + "</strong> / <strong>" + PageCount + "</strong>页 每页<strong>" + PageSize + "</strong> 信息数 <strong>" + RecordCount + "</strong>";
        }

        private void GetPageInfo()
        {
            int CurrentPagePerTen, PagePerTen;
            if((CurrentPageIndex - 1) % 10 == 0)
            {
                CurrentPagePerTen = (CurrentPageIndex - 1) / 10;
            }
            else
            {
                CurrentPagePerTen = ((CurrentPageIndex - 1) - (CurrentPageIndex - 1) % 10) / 10;
            }
            PagePerTen = PageCount / 10;
            string IndexNoLink = "<font face=/"webdings/" color=/"#FF0000/">9</font>";
            string IndexHaveLink = "<a href=/"WebForm1.aspx?page=1/" title=/"首页/"><font face=/"webdings/">9</font></a>";
            string PreTenLink = "<a href=/"WebForm1.aspx?page=" + CurrentPagePerTen * 10 + "/" title=/"上十页/"><font face=/"webdings/">7</font></a> ";
            string NextTenLink = " <a href=/"WebForm1.aspx?page=" + (CurrentPagePerTen * 10 + 11) + "/" title=/"下十页/"><font face=/"webdings/">8</font></a>";
            string LastNoLink = "<font face=/"webdings/" color=/"#FF0000/">:</font>";
            string LastHaveLink = "<a href=/"WebForm1.aspx?page=" + PageCount + "/" title=/"尾页/"><font face=/"webdings/">:</font></a>";
            string PageIndex = "";
            for(int i = CurrentPagePerTen * 10 + 1; i < CurrentPagePerTen * 10 + 11; i++)
            {
                if(i > PageCount)
                {
                    break;
                }
                if(i == CurrentPageIndex)
                {
                    PageIndex += " <font color=/"FF0000/">" + i + "</font>";
                }
                else
                {
                    PageIndex += " <a href=/"WebForm1.aspx?page=" + i + "/">" + i + "</a>";
                }
            }
            if(CurrentPageIndex == 1)
            {
                PageInfo.Text += IndexNoLink;
            }
            else
            {
                PageInfo.Text += IndexHaveLink;
            }
            if(CurrentPagePerTen != 0)
            {
                PageInfo.Text += PreTenLink;
            }
            PageInfo.Text += PageIndex;
            if(CurrentPagePerTen < PagePerTen)
            {
                PageInfo.Text += NextTenLink;
            }
            if(CurrentPageIndex < PageCount)
            {
                PageInfo.Text += LastHaveLink;
            }
            else
            {
                PageInfo.Text += LastNoLink;
            }
        }

        private void Button1_Click(object sender, System.EventArgs e)
        {
            Response.Redirect("WebForm1.aspx?page=" + GoPageIndex.Text);
        }

 

以下示例说明如何用AspNetPager对DataGrid进行分页。
            <%@ Page Language="C#"%>
            <%@ Import Namespace="System.Data"%>
            <%@Import Namespace="System.Data.SqlClient"%>
            <%@Import Namespace="System.Configuration"%>
            <%@Register TagPrefix="Webdiyer" Namespace="Wuqi.Webdiyer" Assembly="aspnetpager"%>
            <HTML>
            <HEAD>
            <TITLE>Welcome to Webdiyer.com </TITLE>
              <script runat="server">
            SqlConnection conn;
            SqlCommand cmd;
            void Page_Load(object src,EventArgs e)
            {
            conn=new SqlConnection(ConfigurationSettings.AppSettings["ConnStr"]);
            if(!Page.IsPostBack)
            {
            cmd=new SqlCommand("GetNews",conn);
            cmd.CommandType=CommandType.StoredProcedure;
            cmd.Parameters.Add("@pageindex",1);
            cmd.Parameters.Add("@pagesize",1);
            cmd.Parameters.Add("@docount",true);
            conn.Open();
            pager.RecordCount=(int)cmd.ExecuteScalar();
            conn.Close();
            BindData();
            }
            }
           
            void BindData()
            {
            cmd=new SqlCommand("GetNews",conn);
            cmd.CommandType=CommandType.StoredProcedure;
            cmd.Parameters.Add("@pageindex",pager.CurrentPageIndex);
            cmd.Parameters.Add("@pagesize",pager.PageSize);
            cmd.Parameters.Add("@docount",false);
            conn.Open();
            dataGrid1.DataSource=cmd.ExecuteReader();
            dataGrid1.DataBind();
            conn.Close();
            pager.CustomInfoText="记录总数:<font color=/"blue/"><b>"+pager.RecordCount.ToString()+"</b></font>";
            pager.CustomInfoText+=" 总页数:<font color=/"blue/"><b>"+pager.PageCount.ToString()+"</b></font>";
            pager.CustomInfoText+=" 当前页:<font color=/"red/"><b>"+pager.CurrentPageIndex.ToString()+"</b></font>";
            }
            void ChangePage(object src,PageChangedEventArgs e)
            {
            pager.CurrentPageIndex=e.NewPageIndex;
            BindData();
            }
              </script>
                 <meta http-equiv="Content-Language" content="zh-cn">
            <meta http-equiv="content-type" content="text/html;charset=gb2312">
            <META NAME="Generator" CONTENT="EditPlus">
            <META NAME="Author" CONTENT="Webdiyer(yhaili@21cn.com)">
            </HEAD>
            <body>
            <form runat="server" ID="Form1">
            <asp:DataGrid id="dataGrid1" runat="server" />
           
            <Webdiyer:AspNetPager id="pager"
            runat="server"
            PageSize="8"
            NumericButtonCount="8"
            ShowCustomInfoSection="left"
            PagingButtonSpacing="0"
            ShowInputBox="always"
            CssClass="mypager"
            HorizontalAlign="right"
            OnPageChanged="ChangePage" 
            SubmitButtonText="转到"
            NumericButtonTextFormatString="[{0}]"/>
           
            </form>
            </body>
            </HTML>
            
下面是该示例所用的Sql Server存储过程:
            CREATE procedure GetNews
             (@pagesize int,
            @pageindex int,
            @docount bit)
            as
            set nocount on
            if(@docount=1)
            select count(id) from news
            else
            begin
            declare @indextable table(id int identity(1,1),nid int)
            declare @PageLowerBound int
            declare @PageUpperBound int
            set @PageLowerBound=(@pageindex-1)*@pagesize
            set @PageUpperBound=@PageLowerBound+@pagesize
            set rowcount @PageUpperBound
            insert into @indextable(nid) select id from news order by addtime desc
            select O.id,O.source,O.title,O.addtime from news O,@indextable t where O.id=t.nid
            and t.id>@PageLowerBound and t.id<=@PageUpperBound order by t.id
            end
            set nocount off
            GO

ASP.NET中Repeater控件和DataList控件通用的分页类

本文利用PagedDataSource+Repeater控件实现数字分页, DataList控件也可以通过修改ID名称实现分页。 /// /// Repeater分页 /// /// DataS...

asp.net之DataList的使用方法,及分页(存储过程创建),编辑,更新,删除

using System; using System.Collections.Generic; using System.Linq; using System.Web; using Syste...

asp.net中Datalist使用数字分页的实现方法

http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> http://www.w3.org/1999/xhtml"> 无标...

asp.net DataList控件分页代码

ASP.NET DataList绑定数据并实现分页

显示当前页码 Label 属性 值 ID NowPageNumberLab text    1 ××××××××××××××××××××××××××××××××××××××××××× 显示...

asp.net用datalist进行分页

前台代码: 这个是前台代码:必须要先设计号界面 ...

ASP.NET中运用DataList控件来实现编辑、更新、删除、取消等功能

1、前台设计               .style2         {             width: 67px;         }           ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:ASP.NET中DataList分页功能
举报原因:
原因补充:

(最多只允许输入30个字)