导航

存储过程:
     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);
        }


测试结果,10W条留言板记录