分页5
来源:互联网 发布:阿里云os电视系统 编辑:程序博客网 时间:2024/04/29 18:12
DataGrid终极分页法(五):让分页可以直接跳转到首页、尾页 原创 smallfools 2006-04-14 10:23:18 查看评论
标签: asp.net
如何给DataGrid加上跳转到首页和尾页的按钮,如下图所示:
0 && image.height>0){if(image.width>=510){this.width=510;this.height=image.height*510/image.width;}}" border=0>
还是在DataGridPage.aspx.cs页面里,找到DataGrid1_ItemCreated事件,在“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);
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 = " ";
pager.Controls.AddAt(2,lo);
lo.Text = " ";
pager.Controls.AddAt(2,lo);
Label l3 = new Label();
l3.Text = " ";
pager.Controls.Add(l3);
l3.Text = " ";
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);
IB4.ImageUrl = "goend.gif";
IB4.ToolTip = "跳转到尾页";
IB4.Click += new System.Web.UI.ImageClickEventHandler(this.IB4_Click);
pager.Controls.Add(IB4);
因为以上代码调用了两个事件:IB1_Click和IB4_Click,所以,要在cs页面里将它们添加上去。
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();
}
{
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();
}
编译后看看效果吧。以下是完整的代码:
DataGridPage.aspx:
<%@ Page language="c#" Codebehind="DataGridPage.aspx.cs" AutoEventWireup="false" Inherits="test.DataGridPage" %>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>DataGridPage</title>
<metaname="GENERATOR"Content="Microsoft Visual Studio .NET 7.1">
<metaname="CODE_LANGUAGE"Content="C#">
<metaname="vs_defaultClientScript"content="JavaScript">
<metaname="vs_targetSchema"content="http://schemas.microsoft.com/intellisense/ie5">
<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;"宋体" } </style>
</HEAD>
<body>
<formid="Form1"method="post"runat="server">
<FONTface="宋体">
<asp:DataGridid="DataGrid1"runat="server"AllowPaging="True">
<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 = " ";
pager.Controls.AddAt(2,lo);
Label l3 = new Label();
l3.Text = " ";
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 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();
}
}
}
标签: asp.net
- 分页5
- 分页逻辑 5 三
- 5UCMS分页样式
- 5种jQuery 分页
- 5 采集分页列表
- 分页
- 分页
- 分页
- 分页
- 分页
- 分页
- 分页
- 分页
- 分页
- 分页
- 分页
- 分页
- 分页
- 反病毒引擎设计之实时监控篇
- 无法在web服务器上启动调试,未将项目配置为进行调试_解决方案
- 分页4
- IBM: Customer Order Analysis and Tracking System:从遗留结构到随需应变
- As3.0与java 5语法对比
- 分页5
- 数据结构(严蔚敏)抽象数据类型Triplet的表示与实现(第一章)
- 如何实现同一用户只能存在一个实例?
- 柯达Z650
- New Challenge
- 分页6
- 管理WebService的状态
- 分页7
- Hibernate应用中遇到的若干问题