All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
用PHP程序实现从数组里筛选出重复的数据_php资料_编程技术
Visual C++ 6.0教程:c++程序的组成之常量与变量
深入理解C#,3.0的五项主要改进_.net资料_编程技术
Delphi建立字体形状窗体
PHP动态网站制作中关于文件操作的疑难问答_php资料_编程技术
Asp.net日期字符串格式化显示方法_[Asp.Net教程]
delphi图像列表组件(TImageList)使用实例
PHP实例:PHP汉字验证码的实现_[PHP教程]
很有用的文件操作php函数_[PHP教程]
C#得到文件路径,几种不同需求_[Asp.Net教程]
Reading number is top 10 pictures
陪睡门马睿菈自曝写真 称首拍大尺度照片2
Thrilling English baby
这只猪到底犯了什么错?
Sora aoi on twitter5
Sexy women in 2013--2
2012 national geographic daily picture5
采访美女孙菲菲
Fat bird to fly
NeedWallpaper1
The cat shit
Download software ranking
WebService在.NET中的实战应用教学视频 → 第5集
The Bermuda triangle1
Tram sex maniac 2 (H) rar bag7
Unix video tutorial9
matrix1
Boxer's Top ten classic battle3
I'm come from Beijing1
虚拟机5.5.3版
Tram sex maniac 2 (H) rar bag17
Kung fu panda - the secret of the teacher
delv published in(发表于) 2014/1/16 9:30:32 Edit(编辑)
一个功能齐全的DataGrid分页例子_[Asp.Net教程]

一个功能齐全的DataGrid分页例子_[Asp.Net教程]

一个功能齐全的DataGrid分页例子_[Asp.Net教程]

面的这个例子提供了DataGrid的分页功能。aspx代码和Cohe Behind代码分别如下,注意更改数据录连接字符串。结果如下:


C#版本 例子


DataGridPaging.aspx

<%@ Page language="c#" EnableViewState = "true" Codebehind="DataGridPaging.aspx.cs"<br/>AutoEventWireup="false" Inherits="eMeng.Exam.DataGridPaging.DataGridPaging" %><br/><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><br/><HTML><br/><HEAD><br/></HEAD><br/><body MS_POSITIONING="GridLayout"><br/><form id="Form1" runat="server"><br/><asp:datagrid id="MyDataGrid" runat="server" AutoGenerateColumns="False"<br/>HorizontalAlign="Center" AlternatingItemStyle-BackColor="#eeeeee"<br/>HeaderStyle-BackColor="#aaaadd" Font-Size="8pt" Font-Name="Verdana"<br/>CellSpacing="0" CellPadding="3" GridLines="Both" BorderWidth="1"<br/>BorderColor="black" OnPageIndexChanged="MyDataGrid_Page" PagerStyle-HorizontalAlign="Right"<br/>PagerStyle-Mode="NumericPages" PageSize="5" AllowPaging="True"><br/><AlternatingItemStyle BackColor="#EEEEEE"></AlternatingItemStyle><br/><HeaderStyle BackColor="#AAAADD" Font-Bold="True" HorizontalAlign="Center"></HeaderStyle><br/><PagerStyle HorizontalAlign="Right" Mode="NumericPages"></PagerStyle><br/><Columns><br/><asp:BoundColumn HeaderText="标题" DataField="Title" HeaderStyle-Width="480px"><br/></asp:BoundColumn><br/><asp:BoundColumn HeaderText="发表日期" DataField="CreateDate" DataFormatString="{0:yyyy-MM-dd hh:mm:ss}"><br/></asp:BoundColumn><br/></Columns><br/></asp:datagrid><br/><p style="FONT-SIZE:9pt" align="center"><br/><asp:label id="lblPageCount" runat="server"></asp:label> <br/><asp:label id="lblCurrentIndex" runat="server"></asp:label><br/><asp:linkbutton id="btnFirst" <I>onclick</I>="PagerButt<I>onClick</I>" runat="server" Font-Name="verdana"<br/>Font-size="8pt" ForeColor="navy" CommandArgument="0"></asp:linkbutton> <br/><asp:linkbutton id="btnPrev" <I>onclick</I>="PagerButt<I>onClick</I>" runat="server" Font-Name="verdana"<br/>Font-size="8pt" ForeColor="navy" CommandArgument="prev"></asp:linkbutton> <br/><asp:linkbutton id="btnNext" <I>onclick</I>="PagerButt<I>onClick</I>" runat="server" Font-Name="verdana"<br/>Font-size="8pt" ForeColor="navy" CommandArgument="next"></asp:linkbutton> <br/><asp:linkbutton id="btnLast" <I>onclick</I>="PagerButt<I>onClick</I>" runat="server" Font-Name="verdana"<br/>Font-size="8pt" ForeColor="navy" CommandArgument="last"></asp:linkbutton><br/></p><br/></form><br/></body><br/></HTML><br/>

DataGridPaging.aspx.cs

using System;<br/>using System.Collections;<br/>using System.ComponentModel;<br/>using System.Data;<br/>using System.Data.OleDb;<br/>using System.Drawing;<br/>using System.Web;<br/>using System.Web.SessionState;<br/>using System.Web.UI;<br/>using System.Web.UI.WebControls;<br/>using System.Web.UI.HtmlControls;<br/>namespace eMeng.Exam.DataGridPaging<br/>{<br/>/// <summary><br/>/// DataGridPaging 的摘要说明。<br/>/// </summary><br/>public class DataGridPaging : System.Web.UI.Page<br/>{<br/>protected System.Web.UI.WebControls.DataGrid MyDataGrid;<br/>protected System.Web.UI.WebControls.Label lblPageCount;<br/>protected System.Web.UI.WebControls.Label lblCurrentIndex;<br/>protected System.Web.UI.WebControls.LinkButton btnFirst;<br/>protected System.Web.UI.WebControls.LinkButton btnPrev;<br/>protected System.Web.UI.WebControls.LinkButton btnNext;<br/>protected System.Web.UI.WebControls.LinkButton btnLast;<br/>private OleDbConnection cn = new OleDbConnection();<br/>private void Page_Load(object sender, System.EventArgs e)<br/>{<br/>// 在此处放置用户代码以初始化页面<br/>btnFirst.Text = "最首页";<br/>btnPrev.Text = "前一页";<br/>btnNext.Text = "下一页";<br/>btnLast.Text = "最后页";<br/>OpenDatabase();<br/>BindGrid();<br/>}<br/>private void OpenDatabase()<br/>{<br/>cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("xxxx.mdb");<br/>cn.Open();<br/>}<br/>private void ShowStats()<br/>{<br/>lblCurrentIndex.Text = "第 " + (MyDataGrid.CurrentPageIndex + 1).ToString() + " 页";<br/>lblPageCount.Text = "总共 " + MyDataGrid.PageCount.ToString() + " 页";<br/>}<br/>public void PagerButt<I>onClick</I>(object sender, EventArgs e)<br/>{<br/>string arg = ((LinkButton)sender).CommandArgument.ToString();<br/>switch(arg)<br/>{<br/>case "next":<br/>if (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1))<br/>{<br/>MyDataGrid.CurrentPageIndex += 1;<br/>}<br/>break;<br/>case "prev":<br/>if (MyDataGrid.CurrentPageIndex > 0)<br/>{<br/>MyDataGrid.CurrentPageIndex -= 1;<br/>}<br/>break;<br/>case "last":<br/>MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1);<br/>break;<br/>default:<br/>MyDataGrid.CurrentPageIndex = System.Convert.ToInt32(arg);<br/>break;<br/>}<br/>BindGrid();<br/>ShowStats();<br/>}<br/>public void BindGrid()<br/>{<br/>OleDbConnection myConnection = cn;<br/>DataSet ds = new DataSet();<br/>OleDbDataAdapter adapter = new OleDbDataAdapter("Select Title,CreateDate from Document", myConnection);<br/>adapter.Fill(ds, "Document");<br/>MyDataGrid.DataSource = ds.Tables["Document"].DefaultView;<br/>MyDataGrid.DataBind();<br/>ShowStats();<br/>}<br/>public void MyDataGrid_Page(object sender, DataGridPageChangedEventArgs e)<br/>{<br/>int startIndex ;<br/>startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize;<br/>MyDataGrid.CurrentPageIndex = e.NewPageIndex;<br/>BindGrid();<br/>ShowStats();<br/>}<br/>#region Web Form Designer generated code<br/>override protected void OnInit(EventArgs e)<br/>{<br/>//<br/>// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。<br/>//<br/>InitializeComponent();<br/>base.OnInit(e);<br/>}<br/>/// <summary><br/>/// 设计器支持所需的方法 - 不要使用代码编辑器修改<br/>/// 此方法的内容。<br/>/// </summary><br/>private void InitializeComponent()<br/>{<br/>this.Load += new System.EventHandler(this.Page_Load);<br/>}<br/>#endregion<br/>}<br/>}<br/>

VB.NET版本


DataGridPaging.aspx

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="DataGridPaging.aspx.vb" Inherits="DataGridPaging"%><br/><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><br/><HTML><br/><HEAD><br/></HEAD><br/><body MS_POSITIONING="GridLayout"><br/><form id="Form1" runat="server"><br/><asp:datagrid id="MyDataGrid" runat="server" AlternatingItemStyle-BackColor="#eeeeee"<br/>HeaderStyle-BackColor="#aaaadd" Font-Size="8pt" Font-Name="Verdana" CellSpacing="0"<br/>CellPadding="3" GridLines="Both" BorderWidth="1" BorderColor="black"<br/>OnPageIndexChanged="MyDataGrid_Page" PagerStyle-HorizontalAlign="Right"<br/>PagerStyle-Mode="NumericPages" PageSize="5" AllowPaging="True"><br/><AlternatingItemStyle BackColor="#EEEEEE"></AlternatingItemStyle><br/><HeaderStyle BackColor="#AAAADD"></HeaderStyle><br/><PagerStyle HorizontalAlign="Right" Mode="NumericPages"></PagerStyle><br/></asp:datagrid><br/><p style="font-size:9pt"><br/><asp:label id="lblPageCount" runat="server"></asp:label> <br/><asp:label id="lblCurrentIndex" runat="server"></asp:label><br/><asp:linkbutton id="btnFirst" <I>onclick</I>="PagerButt<I>onClick</I>" runat="server"<br/>Font-Name="verdana" Font-size="8pt" ForeColor="navy" CommandArgument="0"><br/></asp:linkbutton> <br/><asp:linkbutton id="btnPrev" <I>onclick</I>="PagerButt<I>onClick</I>" runat="server"<br/>Font-Name="verdana" Font-size="8pt" ForeColor="navy" CommandArgument="prev"><br/></asp:linkbutton> <br/><asp:linkbutton id="btnNext" <I>onclick</I>="PagerButt<I>onClick</I>" runat="server"<br/>Font-Name="verdana" Font-size="8pt" ForeColor="navy" CommandArgument="next"><br/></asp:linkbutton> <br/><asp:linkbutton id="btnLast" <I>onclick</I>="PagerButt<I>onClick</I>" runat="server"<br/>Font-Name="verdana" Font-size="8pt" ForeColor="navy" CommandArgument="last"><br/></asp:linkbutton><br/></p><br/></form><br/></body><br/></HTML><br/>

DataGridPaging.aspx.vb

Imports System.Data.SqlClient<br/>Imports System.Data<br/>Imports System.Web.UI<br/>Public Class DataGridPaging<br/>Inherits System.Web.UI.Page<br/>Protected WithEvents MyDataGrid As System.Web.UI.WebControls.DataGrid<br/>Protected WithEvents btnFirst As System.Web.UI.WebControls.LinkButton<br/>Protected WithEvents btnPrev As System.Web.UI.WebControls.LinkButton<br/>Protected WithEvents btnNext As System.Web.UI.WebControls.LinkButton<br/>Protected WithEvents btnLast As System.Web.UI.WebControls.LinkButton<br/>Protected WithEvents lblCurrentIndex As System.Web.UI.WebControls.Label<br/>Protected WithEvents lblPageCount As System.Web.UI.WebControls.Label<br/>Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid<br/>#Region " Web Form Designer Generated Code "<br/>'This call is required by the Web Form Designer.<br/><System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()<br/>End Sub<br/>Private Sub Page_Init(ByVal sender As System.Object,_<br/>ByVal e As System.EventArgs) Handles MyBase.Init<br/>'CODEGEN: This method call is required by the Web Form Designer<br/>'Do not modify it using the code editor.<br/>InitializeComponent()<br/>End Sub<br/>#End Region<br/>Private Sub Page_Load(ByVal sender As System.Object,_<br/>ByVal e As System.EventArgs) Handles MyBase.Load<br/>btnFirst.Text = "最首页"<br/>btnPrev.Text = "前一页"<br/>btnNext.Text = "下一页"<br/>btnLast.Text = "最后页"<br/>OpenDatabase()<br/>BindGrid()<br/>End Sub<br/>Sub MyDataGrid_Page(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs)<br/>Dim startIndex As Integer<br/>startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize<br/>MyDataGrid.CurrentPageIndex = e.NewPageIndex<br/>BindGrid()<br/>ShowStats()<br/>End Sub<br/>Sub BindGrid()<br/>Dim myConnection As SqlConnection = cn<br/>Dim ds As DataSet = New DataSet()<br/>Dim adapter As SqlDataAdapter = New SqlDataAdapter("Select * from Orders", myConnection)<br/>adapter.Fill(ds, "Orders")<br/>MyDataGrid.DataSource = ds.Tables("Orders").DefaultView<br/>MyDataGrid.DataBind()<br/>ShowStats()<br/>End Sub<br/>Sub PagerButt<I>onClick</I>(ByVal sender As Object, ByVal e As EventArgs)<br/>'used by external paging UI<br/>Dim arg As String = sender.CommandArgument<br/>Select Case arg<br/>Case "next"<br/>If (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1)) Then<br/>MyDataGrid.CurrentPageIndex += 1<br/>End If<br/>Case "prev"<br/>If (MyDataGrid.CurrentPageIndex > 0) Then<br/>MyDataGrid.CurrentPageIndex -= 1<br/>End If<br/>Case "last"<br/>MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1)<br/>Case Else<br/>'page number<br/>MyDataGrid.CurrentPageIndex = System.Convert.ToInt32(arg)<br/>End Select<br/>BindGrid()<br/>ShowStats()<br/>End Sub<br/>Sub ShowStats()<br/>lblCurrentIndex.Text = "第 " + (MyDataGrid.CurrentPageIndex + 1).ToString() + " 页"<br/>lblPageCount.Text = "总共 " + MyDataGrid.PageCount.ToString() + " 页"<br/>End Sub<br/>Public cn As New SqlClient.SqlConnection()<br/>Public Sub OpenDatabase()<br/>cn.ConnectionString = "Server=.;Database=NorthWind;User Id=sa;Password=;"<br/>cn.Open()<br/>End Sub<br/>End Class<br/>


方法2:完全后代码写法:例子


DataGridPaging2.aspx

<%@ Page language="c#" EnableViewState = "true" Codebehind="DataGridPaging2.aspx.cs" AutoEventWireup="false"<br/>Inherits="eMeng.Exam.DataGridPaging2.DataGridPaging2" %><br/><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><br/><HTML><br/><HEAD><br/><TITLE>功能齐全的分页的例子【2】</TITLE><br/></HEAD><br/><body MS_POSITIONING="GridLayout"><br/><form id="Form1" runat="server"><br/><asp:datagrid id="MyDataGrid" runat="server" AutoGenerateColumns="False" HorizontalAlign="Center"<br/>AlternatingItemStyle-BackColor="#eeeeee" HeaderStyle-BackColor="#aaaadd" Font-Size="8pt"<br/>Font-Name="Verdana" CellSpacing="0" CellPadding="3" GridLines="Both" BorderWidth="1"<br/>BorderColor="black" PagerStyle-HorizontalAlign="Right" PagerStyle-Mode="NumericPages"<br/>PageSize="5" AllowPaging="True"><br/><AlternatingItemStyle BackColor="#EEEEEE"></AlternatingItemStyle><br/><HeaderStyle BackColor="#AAAADD" Font-Bold="True" HorizontalAlign="Center"></HeaderStyle><br/><PagerStyle HorizontalAlign="Right" Mode="NumericPages"></PagerStyle><br/><Columns><br/><asp:BoundColumn HeaderText="标题" DataField="Title" HeaderStyle-Width="480px"></asp:BoundColumn><br/><asp:BoundColumn HeaderText="发表日期" DataField="CreateDate" <br/>DataFormatString="{0:yyyy-MM-dd hh:mm:ss}"></asp:BoundColumn><br/></Columns><br/></asp:datagrid><br/><p align="center"><br/><asp:label id="lblPageCount" runat="server" Font-Size="9pt" Font-Bold="True"></asp:label> <br/><asp:label id="lblCurrentIndex" runat="server" Font-Size="9pt" Font-Bold="True"></asp:label><br/><asp:linkbutton id="btnFirst" runat="server" Font-size="9pt" ForeColor="navy" CommandArgument="0"/> <br/><asp:linkbutton id="btnPrev" runat="server" Font-size="9pt" ForeColor="navy" CommandArgument="prev"/> <br/><asp:linkbutton id="btnNext" runat="server" Font-size="9pt" ForeColor="navy" CommandArgument="next"/> <br/><asp:linkbutton id="btnLast" runat="server" Font-size="9pt" ForeColor="navy" CommandArgument="last"/><br/></p><br/></form><br/></body><br/></HTML><br/>

DataGridPaging2.aspx.cs

using System;<br/>using System.Collections;<br/>using System.ComponentModel;<br/>using System.Data;<br/>using System.Data.OleDb;<br/>using System.Drawing;<br/>using System.Web;<br/>using System.Web.SessionState;<br/>using System.Web.UI;<br/>using System.Web.UI.WebControls;<br/>using System.Web.UI.HtmlControls;<br/>namespace eMeng.Exam.DataGridPaging2<br/>{<br/>/// <summary><br/>/// DataGridPaging 的摘要说明。<br/>/// </summary><br/>public class DataGridPaging2 : System.Web.UI.Page<br/>{<br/>protected System.Web.UI.WebControls.DataGrid MyDataGrid;<br/>protected System.Web.UI.WebControls.Label lblPageCount;<br/>protected System.Web.UI.WebControls.Label lblCurrentIndex;<br/>protected System.Web.UI.WebControls.LinkButton btnFirst;<br/>protected System.Web.UI.WebControls.LinkButton btnPrev;<br/>protected System.Web.UI.WebControls.LinkButton btnNext;<br/>protected System.Web.UI.WebControls.LinkButton btnLast;<br/>private OleDbConnection cn = new OleDbConnection();<br/>private void Page_Load(object sender, System.EventArgs e)<br/>{<br/>// 在此处放置用户代码以初始化页面<br/>btnFirst.Text = "最首页";<br/>btnPrev.Text = "前一页";<br/>btnNext.Text = "下一页";<br/>btnLast.Text = "最后页";<br/>OpenDatabase();<br/>BindGrid();<br/>}<br/>private void OpenDatabase()<br/>{<br/>cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="<br/>+ HttpContext.Current.Server.MapPath("../../aspxWeb.mdb.ascx");<br/>cn.Open();<br/>}<br/>private void ShowStats()<br/>{<br/>lblCurrentIndex.Text = "第 " + (MyDataGrid.CurrentPageIndex + 1).ToString() + " 页";<br/>lblPageCount.Text = "总共 " + MyDataGrid.PageCount.ToString() + " 页";<br/>}<br/>public void PagerButt<I>onClick</I>(object sender, EventArgs e)<br/>{<br/>string arg = ((LinkButton)sender).CommandArgument.ToString();<br/>switch(arg)<br/>{<br/>case "next":<br/>if (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1))<br/>{<br/>MyDataGrid.CurrentPageIndex += 1;<br/>}<br/>break;<br/>case "prev":<br/>if (MyDataGrid.CurrentPageIndex > 0)<br/>{<br/>MyDataGrid.CurrentPageIndex -= 1;<br/>}<br/>break;<br/>case "last":<br/>MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1);<br/>break;<br/>default:<br/>MyDataGrid.CurrentPageIndex = System.Convert.ToInt32(arg);<br/>break;<br/>}<br/>BindGrid();<br/>ShowStats();<br/>}<br/>public void BindGrid()<br/>{<br/>OleDbConnection myConnection = cn;<br/>DataSet ds = new DataSet();<br/>OleDbDataAdapter adapter = new OleDbDataAdapter("Select Title,CreateDate from Document", myConnection);<br/>adapter.Fill(ds, "Document");<br/>MyDataGrid.DataSource = ds.Tables["Document"].DefaultView;<br/>MyDataGrid.DataBind();<br/>ShowStats();<br/>}<br/>#region Web Form Designer generated code<br/>override protected void OnInit(EventArgs e)<br/>{<br/>//<br/>// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。<br/>//<br/>InitializeComponent();<br/>base.OnInit(e);<br/>}<br/>/// <summary><br/>/// 设计器支持所需的方法 - 不要使用代码编辑器修改<br/>/// 此方法的内容。<br/>/// </summary><br/>private void InitializeComponent()<br/>{<br/>this.MyDataGrid.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.OnPageIndexChanged);<br/>this.btnFirst.Click += new System.EventHandler(this.PagerButt<I>onClick</I>);<br/>this.btnPrev.Click += new System.EventHandler(this.PagerButt<I>onClick</I>);<br/>this.btnNext.Click += new System.EventHandler(this.PagerButt<I>onClick</I>);<br/>this.btnLast.Click += new System.EventHandler(this.PagerButt<I>onClick</I>);<br/>this.Load += new System.EventHandler(this.Page_Load);<br/>}<br/>#endregion<br/>private void OnPageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)<br/>{<br/>int startIndex ;<br/>startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize;<br/>MyDataGrid.CurrentPageIndex = e.NewPageIndex;<br/>BindGrid();<br/>ShowStats();<br/>}<br/>}<br/>}<br/>作者:孟宪会 出自:【孟宪会之精彩世界】





添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.