一个功能齐全的DataGrid分页例子
2007-12-19 16:08:52   来源:   评论:0 点击:

 


C#版本

DataGridPaging.a x

<%@ Page language="c#" EnableViewState = "true" Codebehind="DataGridPaging.a x.c quot;
AutoEventWireup="false" Inherits="eMeng.Exam.DataGridPaging.DataGridPaging" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Tra itional//E quot gt;
<HTML>
<HEAD>
<meta content="Visual Basic 7.0" name="CODE_LANGUAGE" gt;
<meta content="JavaScript" name="vs_defaultClientScript" gt;
<meta content="http://schemas.microsoft.com/intellise e/ie5" name="vs_targetSchema" gt;
</HEAD>
< ody MS_POSITIONING="GridLayout" gt;
<form id="Form1" runat=" erver" gt;
<a :datagrid id="MyDataGrid" runat=" erver" AutoGenerateColum ="False"
HorizontalAlign="Center" AlternatingItemStyle-BackColor="#eeeeee"
HeaderStyle-BackColor="#aaaadd" Font-Size="8pt" Font-Name="Verdana"
Cell acing="0" CellPadding="3" GridLines=" oth" BorderWidth="1"
BorderColor=" lack" O ageIndexChanged="MyDataGrid_Page" PagerStyle-HorizontalAlign="Right"
PagerStyle-Mode=" umericPage quot; PageSize="5" AllowPaging="True" gt;
 <AlternatingItemStyle BackColor="#EEEEEE" gt lt;/AlternatingItemStyle>
 <HeaderStyle BackColor="#AAAADD" Font-Bold="True" HorizontalAlign="Center" gt lt;/HeaderStyle>
 < agerStyle HorizontalAlign="Right" Mode=" umericPage quot gt lt;/PagerStyle>
 <Colum gt;
 <a :BoundColumn HeaderText="标题" DataField="Title" HeaderStyle-Width="480px" gt;
 </a :BoundColum gt;
 <a :BoundColumn HeaderText="发表日期" DataField="CreateDate" DataFormatString="{0:yyyy-MM-dd hh:mm: }" gt;
 </a :BoundColum gt;
 </Colum gt;
</a :datagrid>
<  style="FONT-SIZE:9pt" align="center" gt;
 <a :label id="lblPageCount" runat=" erver" gt lt;/a :label> am 
 <a :label id="lblCurrentIndex" runat=" erver" gt lt;/a :label>
 <a :linkbutton id=" tnFirst" onclick=" agerButtonClick" runat=" erver" Font-Name="verdana"
  Font-size="8pt" ForeColor=" avy" CommandArgument="0" gt lt;/a :linkbutto gt am 
 <a :linkbutton id=" t rev" onclick=" agerButtonClick" runat=" erver" Font-Name="verdana"
  Font-size="8pt" ForeColor=" avy" CommandArgument=" rev" gt lt;/a :linkbutto gt am 
 <a :linkbutton id=" t ext" onclick=" agerButtonClick" runat=" erver" Font-Name="verdana"
  Font-size="8pt" ForeColor=" avy" CommandArgument=" ext" gt lt;/a :linkbutto gt am 
 <a :linkbutton id=" tnLast" onclick=" agerButtonClick" runat=" erver" Font-Name="verdana"
  Font-size="8pt" ForeColor=" avy" CommandArgument="last" gt lt;/a :linkbutto gt;
</ gt;
</form>
</body>
</HTML>

DataGridPaging.a x.cs

using System;
using System.Collectio 
using System.ComponentModel;
using System.Data;
using System.Data.OleD 
using System.Drawing;
using System.We 
using System.Web.Se io tate;
using System.Web.UI;
using System.Web.UI.WebControl 
using System.Web.UI.HtmlControl 

name ace eMeng.Exam.DataGridPaging
{
/// < ummary>
/// DataGridPaging 的摘要说明。
/// </summary>
public cla  DataGridPaging : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid MyDataGrid;
protected System.Web.UI.WebControls.Label lblPageCount;
protected System.Web.UI.WebControls.Label lblCurrentIndex;
protected System.Web.UI.WebControls.LinkButton btnFirst;
protected System.Web.UI.WebControls.LinkButton bt rev;
protected System.Web.UI.WebControls.LinkButton bt ext;
protected System.Web.UI.WebControls.LinkButton btnLast;
private OleDbCo ection cn = new OleDbCo ection();

private void Page_Load(object sender, System.EventArgs e)
{
 // 在此处放置用户代码以初始化页面
 btnFirst.Text = "最首页" 
 bt rev.Text = "前一页" 
 bt ext.Text = "下一页" 
 btnLast.Text = "最后页" 
 OpenDatabase();
 BindGrid();
}
private void OpenDatabase()
{
cn.Co ectio tring = " rovider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.Ma ath("xxxx.md quot;);
cn.Open();
}
private void ShowStats()
{
lblCurrentIndex.Text = "第 " + (MyDataGrid.CurrentPageIndex + 1).ToString() + " 页" 
lblPageCount.Text = "总共 " + MyDataGrid.PageCount.ToString() + " 页" 
}

public void PagerButtonClick(object sender, EventArgs e)
{
string arg = ((LinkButton)sender).CommandArgument.ToString();
switch(arg)
{
 case " ext":
  if (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1))
  {
  MyDataGrid.CurrentPageIndex += 1;
  }
  break;
 case " rev":
  if (MyDataGrid.CurrentPageIndex > 0)
  {
  MyDataGrid.CurrentPageIndex -= 1;
  }
  break;
 case "last":
  MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1);
  break;
 default:
  MyDataGrid.CurrentPageIndex = System.Convert.ToInt32(arg);
  break;
}
BindGrid();
ShowStats();
}
public void BindGrid()
{
OleDbCo ection myCo ection = c 
DataSet ds = new DataSet();
OleDbDataAdapter adapter = new OleDbDataAdapter(" elect Title,CreateDate from Document", myCo ection);
adapter.Fill(ds, "Document");
MyDataGrid.DataSource = ds.Tables["Document"].DefaultView;
MyDataGrid.DataBind();
ShowStats();
}
public void MyDataGrid_Page(object sender, DataGridPageChangedEventArgs e)
{
int startIndex ;
startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize;
MyDataGrid.CurrentPageIndex = e.NewPageIndex;
BindGrid();
ShowStats();
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 A .NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// < ummary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}


VB.NET版本

DataGridPaging.a x

<%@ Page Language="v quot; AutoEventWireup="false" Codebehind="DataGridPaging.a x.v quot; Inherits="DataGridPaging"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Tra itional//E quot gt;
<HTML>
 <HEAD>
  <meta content="Visual Basic 7.0" name="CODE_LANGUAGE" gt;
  <meta content="JavaScript" name="vs_defaultClientScript" gt;
  <meta content="http://schemas.microsoft.com/intellise e/ie5" name="vs_targetSchema" gt;
 </HEAD>
 < ody MS_POSITIONING="GridLayout" gt;
  <form id="Form1" runat=" erver" gt;
   <a :datagrid id="MyDataGrid" runat=" erver" AlternatingItemStyle-BackColor="#eeeeee"
  HeaderStyle-BackColor="#aaaadd" Font-Size="8pt" Font-Name="Verdana" Cell acing="0"
   CellPadding="3" GridLines=" oth" BorderWidth="1" BorderColor=" lack"
  O ageIndexChanged="MyDataGrid_Page" PagerStyle-HorizontalAlign="Right"
  PagerStyle-Mode=" umericPage quot; PageSize="5" AllowPaging="True" gt;
    <AlternatingItemStyle BackColor="#EEEEEE" gt lt;/AlternatingItemStyle>
    <HeaderStyle BackColor="#AAAADD" gt lt;/HeaderStyle>
    < agerStyle HorizontalAlign="Right" Mode=" umericPage quot gt lt;/PagerStyle>
   </a :datagrid>
   <  style="font-size:9pt" gt;
    <a :label id="lblPageCount" runat=" erver" gt lt;/a :label> am 
    <a :label id="lblCurrentIndex" runat=" erver" gt lt;/a :label>
    <a :linkbutton id=" tnFirst" onclick=" agerButtonClick" runat=" erver"
  Font-Name="verdana" Font-size="8pt" ForeColor=" avy" CommandArgument="0" gt;
  </a :linkbutto gt am 
    <a :linkbutton id=" t rev" onclick=" agerButtonClick" runat=" erver"
  Font-Name="verdana" Font-size="8pt" ForeColor=" avy" CommandArgument=" rev" gt;
  </a :linkbutto gt am 
    <a :linkbutton id=" t ext" onclick=" agerButtonClick" runat=" erver"
  Font-Name="verdana" Font-size="8pt" ForeColor=" avy" CommandArgument=" ext" gt;
  </a :linkbutto gt am 
    <a :linkbutton id=" tnLast" onclick=" agerButtonClick" runat=" erver"
  Font-Name="verdana" Font-size="8pt" ForeColor=" avy" CommandArgument="last" gt;
  </a :linkbutto gt;
   </ gt;
  </form>
 </body>
</HTML>

DataGridPaging.a x.vb

Imports System.Data.SqlClient
Imports System.Data
Imports System.Web.UI

Public Cla  DataGridPaging
 Inherits System.Web.UI.Page

 Protected WithEvents MyDataGrid As System.Web.UI.WebControls.DataGrid
 Protected WithEvents btnFirst As System.Web.UI.WebControls.LinkButton
 Protected WithEvents bt rev As System.Web.UI.WebControls.LinkButton
 Protected WithEvents bt ext As System.Web.UI.WebControls.LinkButton
 Protected WithEvents btnLast As System.Web.UI.WebControls.LinkButton
 Protected WithEvents lblCurrentIndex As System.Web.UI.WebControls.Label
 Protected WithEvents lblPageCount As System.Web.UI.WebControls.Label
 Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid

#Region " Web Form Designer Generated Code "

 'This call is required by the Web Form Designer.
 < ystem.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

 End Sub

 Private Sub Page_Init(ByVal sender As System.Object,_
  ByVal e As System.EventArgs) Handles MyBase.Init
  'CODEGEN: This method call is required by the Web Form Designer
  'Do not modify it using the code editor.
  InitializeComponent()
 End Sub

#End Region

 Private Sub Page_Load(ByVal sender As System.Object,_
  ByVal e As System.EventArgs) Handles MyBase.Load
  btnFirst.Text = "最首页"
  bt rev.Text = "前一页"
  bt ext.Text = "下一页"
  btnLast.Text = "最后页"
  OpenDatabase()
  BindGrid()
 End Sub


 Sub MyDataGrid_Page(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs)
  Dim startIndex As Integer
  startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize
  MyDataGrid.CurrentPageIndex = e.NewPageIndex
  BindGrid()
  ShowStats()
 End Sub

 Sub BindGrid()
  Dim myCo ection As SqlCo ection = cn
  Dim ds As DataSet = New DataSet()
  Dim adapter As SqlDataAdapter = New SqlDataAdapter(" elect * from Order quot;, myCo ection)
  adapter.Fill(ds, "Order quot;)
  MyDataGrid.DataSource = ds.Tables("Order quot;).DefaultView
  MyDataGrid.DataBind()
  ShowStats()

 End Sub

 Sub PagerButtonClick(ByVal sender As Object, ByVal e As EventArgs)
  'used by external paging UI
  Dim arg As String = sender.CommandArgument

  Select Case arg
   Case " ext"
    If (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1)) Then
     MyDataGrid.CurrentPageIndex += 1
    End If
   Case " rev"
    If (MyDataGrid.CurrentPageIndex > 0) Then
     MyDataGrid.CurrentPageIndex -= 1
    End If
   Case "last"
    MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1)
   Case Else
    'page number
    MyDataGrid.CurrentPageIndex = System.Convert.ToInt32(arg)
  End Select
  BindGrid()
  ShowStats()
 End Sub

 Sub ShowStats()
  lblCurrentIndex.Text = "第 " + (MyDataGrid.CurrentPageIndex + 1).ToString() + " 页"
  lblPageCount.Text = "总共 " + MyDataGrid.PageCount.ToString() + " 页"
 End Sub

Public cn As New SqlClient.SqlCo ection()
 Public Sub OpenDatabase()
  cn.Co ectio tring = " erver=.;Database=NorthWind;User Id=sa a word= quot;
  cn.Open()
 End Sub
End Cla 

相关热词搜索:一个功能齐全的DataGrid分页例子

上一篇:在ASP.NET程序中创建唯一序号
下一篇:教你在asp.net中动态变更CSS

分享到: 收藏
频道总排行
频道本月排行