实现数字分页

 

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

 

None.gif using  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}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值