实现数字分页

本文介绍了一个基于 ASP.NET 的 Web 应用中如何实现数据分页功能,通过使用 DataGrid 控件结合 SQL Server 数据库进行数据展示及翻页操作。文章详细展示了页面布局结构、后端代码逻辑以及分页导航的实现方式。

 

 1ExpandedBlockStart.gifContractedBlock.gif<%dot.gif@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="DataGridPage.WebForm1" %>
 2None.gif<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
 3None.gif<HTML>
 4None.gif    <HEAD>
 5None.gif        <title>WebForm1</title>
 6None.gif        <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
 7None.gif        <meta content="C#" name="CODE_LANGUAGE">
 8None.gif        <meta content="JavaScript" name="vs_defaultClientScript">
 9None.gif        <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
10None.gif    </HEAD>
11None.gif    <body MS_POSITIONING="GridLayout">
12None.gif        <form id="Form1" method="post" runat="server">
13None.gif            <table cellSpacing="0" cellPadding="0" align="center" border="1">
14None.gif                <tr>
15None.gif                    <td><asp:datagrid id="DataGrid1" runat="server" AllowPaging="True" Width="502px" PageSize="3">
16None.gif                            <PagerStyle NextPageText="" PrevPageText=""></PagerStyle>
17None.gif                        </asp:datagrid></td>
18None.gif                </tr>
19None.gif                <tr>
20None.gif                    <td style="HEIGHT: 25px" align="center"><asp:linkbutton id="btnFirst" onclick="PagerButtonClick" runat="server" CommandArgument="first">首 页</asp:linkbutton><asp:linkbutton id="btnPrev" onclick="PagerButtonClick" runat="server" CommandArgument="prev">上一页  </asp:linkbutton><asp:linkbutton id="btnNext" onclick="PagerButtonClick" runat="server" CommandArgument="next">下一页</asp:linkbutton><asp:linkbutton id="btnLast" onclick="PagerButtonClick" runat="server" CommandArgument="last">尾 页</asp:linkbutton><asp:label id="LblCurrentIndex" runat="server"></asp:label><asp:label id="LblPageCount" runat="server"></asp:label><asp:label id="LblRecordCount" runat="server"></asp:label></td>
21None.gif                </tr>
22None.gif                <tr>
23None.gif                    <td>
24None.gif                        <div style="FLOAT:left;WIDTH:100%;HEIGHT:20px" runat="server" id="div1"></div>
25None.gif                    </td>
26None.gif                </tr>
27None.gif            </table>
28None.gif        </form>
29None.gif    </body>
30None.gif</HTML>
31None.gif

 

None.gifusing System;
None.gif
using System.Collections;
None.gif
using System.ComponentModel;
None.gif
using System.Data;
None.gif
using System.Drawing;
None.gif
using System.Web;
None.gif
using System.Web.SessionState;
None.gif
using System.Web.UI;
None.gif
using System.Web.UI.WebControls;
None.gif
using System.Web.UI.HtmlControls;
None.gif
using System.Data.SqlClient;
None.gif
namespace DataGridPage
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// WebForm1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class WebForm1 : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected System.Web.UI.WebControls.LinkButton btnFirst;
InBlock.gif        
protected System.Web.UI.WebControls.LinkButton btnPrev;
InBlock.gif        
protected System.Web.UI.WebControls.LinkButton btnNext;
InBlock.gif        
protected System.Web.UI.WebControls.LinkButton btnLast;
InBlock.gif        
protected System.Web.UI.WebControls.Label LblCurrentIndex;
InBlock.gif        
protected System.Web.UI.WebControls.Label LblPageCount;
InBlock.gif        
protected System.Web.UI.WebControls.Label LblRecordCount;
InBlock.gif        
protected System.Web.UI.HtmlControls.HtmlGenericControl div1;
InBlock.gif        
protected System.Web.UI.WebControls.DataGrid DataGrid1;
InBlock.gif    
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 在此处放置用户代码以初始化页面
InBlock.gif
            if(! this.IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif            
InBlock.gif                
string page=Request["page"];
InBlock.gif    
InBlock.gif
InBlock.gif                
if(page != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.DataGrid1.CurrentPageIndex=Convert.ToInt32(Request["page"].ToString())-1;
InBlock.gif                    bind();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif    bind();
InBlock.gif                
ExpandedSubBlockEnd.gif                }

InBlock.gif            
InBlock.gif
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
void bind()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
InBlock.gif            
InBlock.gif            SqlConnection conn
=new SqlConnection("server=.;database=pubs;uid=sa;pwd=;");
InBlock.gif            conn.Open();
InBlock.gif            SqlDataAdapter da
=new SqlDataAdapter("select * from employee",conn);
InBlock.gif            DataSet ds
=new DataSet();
InBlock.gif            da.Fill(ds);
InBlock.gif            
this.DataGrid1.DataSource=ds.Tables[0];
InBlock.gif            
this.DataGrid1.DataBind();
InBlock.gif            
this.LblCurrentIndex.Text=""+(this.DataGrid1.CurrentPageIndex+1)+"";
InBlock.gif            
this.LblPageCount.Text="共:"+this.DataGrid1.PageCount.ToString()+"";
InBlock.gif            
this.LblRecordCount.Text="总共"+ds.Tables[0].Rows.Count.ToString()+"";
InBlock.gif            
if(ds.Tables[0].Rows.Count==0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.btnFirst.Visible=false;
InBlock.gif                
this.btnPrev.Visible=false;
InBlock.gif                
this.btnNext.Visible=false;
InBlock.gif                
this.btnLast.Visible=false;
InBlock.gif                
this.LblCurrentIndex.Visible=false;
InBlock.gif                
this.LblPageCount.Visible=false;
InBlock.gif                
this.LblRecordCount.Visible=false;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
InBlock.gif                
InBlock.gif                
if(this.DataGrid1.PageCount ==1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.btnFirst.Visible=false;
InBlock.gif                
this.btnPrev.Visible=false;
InBlock.gif                
this.btnNext.Visible=false;
InBlock.gif                
this.btnLast.Visible=false;
ExpandedSubBlockEnd.gif            }

InBlock.gif            btnFirst.CommandName 
= "1";
InBlock.gif            btnPrev.CommandName 
= (this.DataGrid1.CurrentPageIndex == 0 ? "1" : this.DataGrid1.CurrentPageIndex.ToString());
InBlock.gif
InBlock.gif            btnNext.CommandName 
= (this.DataGrid1.PageCount ==1 ? this.DataGrid1.PageCount.ToString() : (this.DataGrid1.CurrentPageIndex + 2).ToString());
InBlock.gif            btnLast.CommandName 
=this.DataGrid1.PageCount.ToString();
InBlock.gif        
InBlock.gif            
this.div1.InnerHtml=set_daohang();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif        
protected void PagerButtonClick(object sender,EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(Convert.ToInt32(((LinkButton)sender).CommandName) >  this.DataGrid1.PageCount)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif            ((LinkButton)sender).CommandName
=this.DataGrid1.PageCount.ToString();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
this.DataGrid1.CurrentPageIndex=Convert.ToInt32(((LinkButton)sender).CommandName)-1;
InBlock.gif            bind();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
protected string set_daohang()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string daohang1=null;
InBlock.gif        
InBlock.gif            
//Response.Write(this.DataGrid1.CurrentPageIndex.ToString());
InBlock.gif
            int l=(this.DataGrid1.CurrentPageIndex+1)/10;
InBlock.gif            
string fenye;
InBlock.gif            
for(int i=l*10+1;i<=(l+1)*10  && i < this.DataGrid1.PageCount+1;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
InBlock.gif                
if(i==this.DataGrid1.CurrentPageIndex+1)    
InBlock.gif                    fenye
="<FONT color='red'>"+i+"</FONT>";
InBlock.gif                
else
InBlock.gif                    fenye
="<FONT color='#0000ff'>"+i+"</FONT>";
InBlock.gif                
InBlock.gif                daohang1 
+="&nbsp;<A href='WebForm1.aspx?page="+i+"'><font color='#0000ff' >"+fenye+"</font></a>";
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return daohang1;
InBlock.gif    
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值