分页

DataGrid终极分页法(一):简单分页
 原创  smallfools2006-04-13 15:46:18查看评论  
  
    首先,在.net的项目里新建一个DataGridPage.aspx文件,在aspx文件里拖入一个DataGrid控件。在控件上点击右键,选择“属性生成器”
    选择分页项,勾上“允许分页”,点击确定。
    进入cs页面,先加上“using System.Data.OleDb;”的引用。然后在“public class DataGridPage : System.Web.UI.Page”下加入“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();
}
    然后建个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;
 this.DataGrid1.DataSource = dv;
 this.DataGrid1.DataBind();
}
    在DataGrid属性的PageIndexChang上双击,将会自动跳到cs文件上。
    在cs文件里的DataGrid1_PageIndexChanged事件里输入以下代码:
this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGridBind();
    编译后预览一下,结果如下:
查看更多精彩图片0 && image.height>0){if(image.width>=510){this.width=510;this.height=image.height*510/image.width;}}" border=0>

    点击<、>可以向上或向下翻页。

以下是完整的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>
         <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"></asp:DataGrid></FONT>
         </form>
     </body>
</ HTML >
 
 
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;
              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.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();
         }
     }
}
 

标签:   asp.net  

票数:0
我顶什么是“我顶”?

【发起辩论】  [手机订阅]  [收藏到我的网摘]
[推荐] | [评论] | [投诉] | [打印]
本文章被推荐到了0个圈子
点击数: 469
评论数: 3

本文章引用通告地址(TrackBack Ping URL)为:
http://post.blog.hexun.com/smallfools/trackback.aspx?articleid=3137006
本文章尚未被引用。

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值