分页7

DataGrid终极分页法(七):让分页可以直接跳转某一页
 原创  smallfools2006-04-14 11:18:39查看评论  
  
    当DataGrid的分页很多的时候,往往页导航不能显示所有页数,那么如何让DataGrid可以直接跳到某一页呢?如下图所示:


    还是在DataGridPage.aspx.cs文件里找到“DataGrid1_ItemCreated”事件,在“pager.Controls.Add(IB4);”后面加上以下代码:
Label l4 = new Label();
l4.Text = "  转到第";
pager.Controls.Add(l4);
TextBox tb = new TextBox();
tb.Width = 30;
tb.ID = "tb";
pager.Controls.Add(tb);
Label l5 = new Label();
l5.Text = "页";
pager.Controls.Add(l5);
Button bt = new Button();
bt.Text = "跳转";
bt.Click += new System.EventHandler(this.bt_Click);
pager.Controls.Add(bt);
    因为上面代码里调用了bt_Click事件,所以还要把这个事件给加上。
private void bt_Click(object sender, System.EventArgs e)
{
 int PagaIndex = -1;
 try
 {
  PagaIndex = int.Parse(((TextBox)this.DataGrid1.Controls[0].Controls[this.DataGrid1.Controls[0].Controls.Count-1].Controls[0].FindControl("tb")).Text);
 }
 catch
 {}
 if (PagaIndex>this.DataGrid1.PageCount)
 {
  PagaIndex = this.DataGrid1.PageCount;
 }
 PagaIndex = PagaIndex-1;
 if (PagaIndex<0)
 {
  Page.RegisterClientScriptBlock("errx","<script language=javascript>alert('页码输入有误,请重输')</script>");
 }
 else
 {
  this.DataGrid1.CurrentPageIndex = PagaIndex;
  DataGridBind();
 }
}
    编译后可以看到效果。以下是完整的代码:
DataGridPage.aspx:
<%@ Page language="c#" Codebehind="DataGridPage.aspx.cs" AutoEventWireup="false" Inherits="test.DataGridPage" %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
< HTML >
     <HEAD>
         <title>DataGridPage</title>
         <metacontent="Microsoft Visual Studio .NET 7.1"name="GENERATOR">
         <metacontent="C#"name="CODE_LANGUAGE">
         <metacontent="JavaScript"name="vs_defaultClientScript">
         <metacontent="http://schemas.microsoft.com/intellisense/ie5"name="vs_targetSchema">
         <styletype="text/css">BODY {
     FONT-SIZE: 9pt; BACKGROUND-COLOR: #ffffff
}
A:visited {
     COLOR: #000000; TEXT-DECORATION: none
}
A:active {
     COLOR: #000000; TEXT-DECORATION: none
}
A:hover {
     LEFT: 1px; COLOR: #000000; POSITION: relative; TOP: 1px; TEXT-DECORATION: underline
}
A:link {
     COLOR: #000000; TEXT-DECORATION: none
}
TD {
     FONT-SIZE: 9pt; FONT-FAMILY: " 宋体"
}
         </style>
     </HEAD>
     <body>
         <formid="Form1"method="post"runat="server">
              <FONTface=" 宋体">
                   <asp:datagridid="DataGrid1"runat="server"AllowPaging="True"Width="60%">
                       <PagerStyleVerticalAlign="Middle"HorizontalAlign="Right"Mode="NumericPages"></PagerStyle>
                   </asp:datagrid></FONT></form>
     </body>
</ HTML >
 
DataGridPage.aspx.cs:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
 
namespace test
{
     ///<summary>
     /// DataGridPage 的摘要说明。
     ///</summary>
     public class DataGridPage : System.Web.UI.Page
     {
         protected System.Web.UI.WebControls.DataGrid DataGrid1;
         protected OleDbConnection conn = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=C://Program Files//Microsoft Office//OFFICE11//SAMPLES//Northwind.mdb");
    
         private void Page_Load(object sender, System.EventArgs e)
         {
              if (!this.IsPostBack)
              {
                   DataGridBind();
                  
              }
         }
 
         private void DataGridBind()
         {
              string strSql = "select * from 产品";
              DataSet ds = new DataSet();
              conn.Open();
              OleDbDataAdapter myAdapter = new OleDbDataAdapter(strSql,conn);
              myAdapter.Fill(ds,"ds");
              conn.Close();
 
              DataView dv = ds.Tables[0].DefaultView;
              ViewState["Row"] = dv.Count;
              this.DataGrid1.DataSource = dv;
              this.DataGrid1.DataBind();
         }
 
         #region Web 窗体设计器生成的代码
         override protected void OnInit(EventArgs e)
         {
              //
              // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
              //
              InitializeComponent();
              base.OnInit(e);
         }
        
         ///<summary>
         /// 设计器支持所需的方法 - 不要使用代码编辑器修改
         /// 此方法的内容。
         ///</summary>
         private void InitializeComponent()
         {   
              this.DataGrid1.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemCreated);
              this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
              this.Load += new System.EventHandler(this.Page_Load);
 
         }
         #endregion
 
         private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
         {
              this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
              DataGridBind();
         }
 
         private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
         {
              ListItemType elemType = e.Item.ItemType;
 
              if (elemType == ListItemType.Pager)
              {
                   TableCell pager = (TableCell)e.Item.Controls[0];
                   int up = 0;
                   int down = 0;
                   for (int i=0;i<pager.Controls.Count;i+=2)
                   {
                       Object o = pager.Controls[i];
                       if (o is LinkButton)
                       {
                            LinkButton h = (LinkButton) o;
                            if (h.Text!="...")
                            {
                                 h.ToolTip = " 跳转到第"+h.Text+"页";
                            }
                            if (i==2)
                            {
                                 up = int.Parse(h.Text)-1;
                            }
                            if (i==pager.Controls.Count-3)
                            {
                                 down = int.Parse(h.Text)+1;
                            }
                       }
                       else
                       {
                            Label l = (Label) o;
 
                            if (i==2)
                            {
                                 up = int.Parse(l.Text)-1;
                            }
                            if (i==pager.Controls.Count-3)
                            {
                                 down = int.Parse(l.Text)+1;
                            }
 
                            l.Text = "["+l.Text+"]";
                            l.ForeColor = System.Drawing.Color.Red;
                       }
                   }
 
                   Object oo = pager.Controls[0];
                   if (oo is LinkButton)
                   {
                        LinkButton h = (LinkButton) oo;
                       if (h.Text=="...")
                       {
                            h.ToolTip = " 跳转到第"+up.ToString()+"页";
                       }
                   }
 
                   Object ooo = pager.Controls[pager.Controls.Count-1];
                   if (ooo is LinkButton)
                   {
                       LinkButton h = (LinkButton) ooo;
                       if (h.Text=="...")
                       {
                            h.ToolTip = " 跳转到第"+down.ToString()+"页";
                       }
                   }
 
                   Label la = new Label();
                   la.Text = "<table width=100%><tr><td align=left> 总记录数:<b>"+ViewState["Row"].ToString()+" </b>";
                   la.Text += " 总页数:<b>"+this.DataGrid1.PageCount+" </b>";
                   la.Text += " 当前页:<font color=red><b>"+(this.DataGrid1.CurrentPageIndex+1).ToString()+" </b></font></td><td align=right>";
                   pager.Controls.AddAt(0,la);
 
                   ImageButton IB1 = new ImageButton();
                   IB1.ImageUrl = "gotop.gif";
                   IB1.ToolTip = " 跳转到首页";
                   IB1.Click += new System.Web.UI.ImageClickEventHandler(this.IB1_Click);
                   pager.Controls.AddAt(1,IB1);
 
                   Label lo = new Label();
                   lo.Text = "&nbsp;&nbsp;";
                   pager.Controls.AddAt(2,lo);
 
                   if (this.DataGrid1.CurrentPageIndex>0)
                   {
                       ImageButton IB2 = new ImageButton();
                       IB2.ImageUrl = "goup.gif";
                       IB2.ToolTip = " 上一页";
                       IB2.Click += new System.Web.UI.ImageClickEventHandler(this.IB2_Click);
                       pager.Controls.AddAt(3,IB2);
 
                       Label l1 = new Label();
                       l1.Text = "&nbsp;&nbsp;";
                       pager.Controls.AddAt(4,l1);
                   }
 
                   if (this.DataGrid1.CurrentPageIndex<this.DataGrid1.PageCount-1)
                   {
                       Label l2 = new Label();
                       l2.Text = "&nbsp;&nbsp;";
                       pager.Controls.Add(l2);
 
                       ImageButton IB3 = new ImageButton();
                       IB3.ImageUrl = "gonext.gif";
                       IB3.ToolTip = " 下一页";
                       IB3.Click += new System.Web.UI.ImageClickEventHandler(this.IB3_Click);
                       pager.Controls.Add(IB3);
                   }
 
                   Label l3 = new Label();
                   l3.Text = "&nbsp;&nbsp;";
                   pager.Controls.Add(l3);
 
                   ImageButton IB4 = new ImageButton();
                   IB4.ImageUrl = "goend.gif";
                   IB4.ToolTip = " 跳转到尾页";
                   IB4.Click += new System.Web.UI.ImageClickEventHandler(this.IB4_Click);
                   pager.Controls.Add(IB4);
 
                   Label l4 = new Label();
                   l4.Text = "&nbsp;&nbsp; 转到第";
                   pager.Controls.Add(l4);
 
                   TextBox tb = new TextBox();
                   tb.Width = 30;
                   tb.ID = "tb";
                   pager.Controls.Add(tb);
 
                   Label l5 = new Label();
                   l5.Text = " 页";
                   pager.Controls.Add(l5);
 
                   Button bt = new Button();
                   bt.Text = " 跳转";
                   bt.Click += new System.EventHandler(this.bt_Click);
                   pager.Controls.Add(bt);
 
                   Label lb = new Label();
                   lb.Text = "</td></tr></table>";
                   pager.Controls.Add(lb);
              }
         }
 
         // 跳转到首页
         private void IB1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
         {
              this.DataGrid1.CurrentPageIndex = 0;
              DataGridBind();
         }
 
         // 尾页
         private void IB4_Click(object sender, System.Web.UI.ImageClickEventArgs e)
         {
              int PagaIndex = this.DataGrid1.PageCount-1;
              this.DataGrid1.CurrentPageIndex = PagaIndex;
              DataGridBind();
         }
 
         // 上一页
         private void IB2_Click(object sender, System.Web.UI.ImageClickEventArgs e)
         {
              int PagaIndex = this.DataGrid1.CurrentPageIndex-1;
              this.DataGrid1.CurrentPageIndex = PagaIndex;
              DataGridBind();
         }
 
         // 下一页
         private void IB3_Click(object sender, System.Web.UI.ImageClickEventArgs e)
         {
              int PagaIndex = this.DataGrid1.CurrentPageIndex+1;
              this.DataGrid1.CurrentPageIndex = PagaIndex;
              DataGridBind();
         }
 
         // 跳转到第几页
         private void bt_Click(object sender, System.EventArgs e)
         {
              int PagaIndex = -1;
              try
              {
                   PagaIndex = int.Parse(((TextBox)this.DataGrid1.Controls[0].Controls[this.DataGrid1.Controls[0].Controls.Count-1].Controls[0].FindControl("tb")).Text);
              }
              catch
              {}
 
              if (PagaIndex>this.DataGrid1.PageCount)
              {
                   PagaIndex = this.DataGrid1.PageCount;
              }
 
              PagaIndex = PagaIndex-1;
 
              if (PagaIndex<0)
              {
                   Page.RegisterClientScriptBlock("errx","<script language=javascript>alert(' 页码输入有误,请重输')</script>");
              }
              else
              {
                   this.DataGrid1.CurrentPageIndex = PagaIndex;
                   DataGridBind();
              }
         }
     }
}
 

查看更多精彩图片0 && image.height>0){if(image.width>=510){this.width=510;this.height=image.height*510/image.width;}}" border=0>
 

标签:   asp.net  

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值