DataGrid分页技巧

WebForm1.aspx
None.gif < %@ Page  language ="c#"  EnableViewState  = "true"  Codebehind ="DataGridPaging.aspx.cs"
None.gif AutoEventWireup
="false"  Inherits ="eMeng.Exam.DataGridPaging.DataGridPaging"  % >
None.gif
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
None.gif
< HTML >
None.gif    
< HEAD >
None.gif        
< meta  content ="Visual Basic 7.0"  name ="CODE_LANGUAGE" >
None.gif        
< meta  content ="JavaScript"  name ="vs_defaultClientScript" >
None.gif        
< meta  content ="http://schemas.microsoft.com/intellisense/ie5"  name ="vs_targetSchema" >
None.gif    
</ HEAD >
None.gif    
< body  MS_POSITIONING ="GridLayout" >
None.gif        
< form  id ="Form1"  runat ="server" >
None.gif            
< asp:datagrid  id ="MyDataGrid"  runat ="server"  AutoGenerateColumns ="False"  HorizontalAlign ="Center"
None.gif                AlternatingItemStyle-BackColor
="#eeeeee"  HeaderStyle-BackColor ="#aaaadd"  Font-Size ="8pt"  Font-Name ="Verdana"
None.gif                CellPadding
="3"  BorderWidth ="1px"  BorderColor ="Black"  OnPageIndexChanged ="MyDataGrid_Page"
None.gif                PagerStyle-HorizontalAlign
="Right"  PagerStyle-Mode ="NumericPages"  PageSize ="5"  AllowPaging ="True"
None.gif                Font-Names
="Verdana" >
None.gif                
< AlternatingItemStyle  BackColor ="#EEEEEE" ></ AlternatingItemStyle >
None.gif                
< HeaderStyle  Font-Bold ="True"  HorizontalAlign ="Center"  BackColor ="#AAAADD" ></ HeaderStyle >
None.gif                
< Columns >
None.gif                    
< asp:BoundColumn  DataField ="Title"  HeaderText ="标题" >
None.gif                        
< HeaderStyle  Width ="450px" ></ HeaderStyle >
None.gif                    
</ asp:BoundColumn >
None.gif                    
< asp:BoundColumn  DataField ="pubdate"  HeaderText ="发表日期"  DataFormatString ="{0:yyyy-MM-dd hh:mm:ss}" >
None.gif                        
< HeaderStyle  Width ="70px" ></ HeaderStyle >
None.gif                    
</ asp:BoundColumn >
None.gif                    
< asp:ButtonColumn  Text ="选择"  CommandName ="Select" >
None.gif                        
< HeaderStyle  Width ="30px" ></ HeaderStyle >
None.gif                    
</ asp:ButtonColumn >
None.gif                
</ Columns >
None.gif                
< PagerStyle  HorizontalAlign ="Right"  PageButtonCount ="5"  Mode ="NumericPages" ></ PagerStyle >
None.gif            
</ asp:datagrid >
None.gif            
< style ="FONT-SIZE:9pt"  align ="center" >
None.gif                
< asp:label  id ="lblPageCount"  runat ="server" ></ asp:label > &nbsp;
None.gif                
< asp:label  id ="lblCurrentIndex"  runat ="server" ></ asp:label >
None.gif                
< asp:linkbutton  id ="btnFirst"  onclick ="PagerButtonClick"  runat ="server"  Font-Name ="verdana"  Font-size ="8pt"
None.gif                    ForeColor
="navy"  CommandArgument ="0" ></ asp:linkbutton > &nbsp;
None.gif                
< asp:linkbutton  id ="btnPrev"  onclick ="PagerButtonClick"  runat ="server"  Font-Name ="verdana"  Font-size ="8pt"
None.gif                    ForeColor
="navy"  CommandArgument ="prev" ></ asp:linkbutton > &nbsp;
None.gif                
< asp:linkbutton  id ="btnNext"  onclick ="PagerButtonClick"  runat ="server"  Font-Name ="verdana"  Font-size ="8pt"
None.gif                    ForeColor
="navy"  CommandArgument ="next" ></ asp:linkbutton > &nbsp;
None.gif                
< asp:linkbutton  id ="btnLast"  onclick ="PagerButtonClick"  runat ="server"  Font-Name ="verdana"  Font-size ="8pt"
None.gif                    ForeColor
="navy"  CommandArgument ="last" ></ asp:linkbutton >
None.gif            
</ p >
None.gif        
</ form >
None.gif    
</ body >
None.gif
</ HTML >
None.gif

WebForm1.aspx.cs
None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Data.OleDb;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
None.gif
namespace  eMeng.Exam.DataGridPaging
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// DataGridPaging 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class DataGridPaging : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected System.Web.UI.WebControls.DataGrid MyDataGrid;
InBlock.gif        
protected System.Web.UI.WebControls.Label lblPageCount;
InBlock.gif        
protected System.Web.UI.WebControls.Label lblCurrentIndex;
InBlock.gif        
protected System.Web.UI.WebControls.LinkButton btnFirst;
InBlock.gif        
protected System.Web.UI.WebControls.LinkButton btnPrev;
InBlock.gif        
protected System.Web.UI.WebControls.LinkButton btnNext;
InBlock.gif        
protected System.Web.UI.WebControls.LinkButton btnLast;
InBlock.gif        
private OleDbConnection cn = new OleDbConnection();
InBlock.gif
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 在此处放置用户代码以初始化页面
InBlock.gif
            btnFirst.Text = "最首页";
InBlock.gif            btnPrev.Text 
= "前一页";
InBlock.gif            btnNext.Text 
= "下一页";
InBlock.gif            btnLast.Text 
= "最后页";
InBlock.gif            OpenDatabase();
InBlock.gif            ShowStats();                                                      
InBlock.gif            BindGrid();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void OpenDatabase()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            cn.ConnectionString 
= @"Auto Translate=True;User ID=sa;Tag with column collation when possible=False;
InBlock.gif                                    Data Source='192.168.0.99';Password=4703;Initial Catalog=pubs;Use Procedure for Prepare=1;
InBlock.gif                                    Provider='SQLOLEDB.1';Persist Security Info=True;Workstation ID=DNB07;Use Encryption for Data=False;
InBlock.gif                                    Packet Size=4096
";
InBlock.gif
InBlock.gif            cn.Open();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void ShowStats()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            lblCurrentIndex.Text 
= "第 " + (MyDataGrid.CurrentPageIndex + 1).ToString() + " 页";
InBlock.gif            lblPageCount.Text 
= "总共 " + MyDataGrid.PageCount.ToString() + " 页";
InBlock.gif            btnFirst.Enabled 
= !(MyDataGrid.CurrentPageIndex == 0);
InBlock.gif            btnPrev.Enabled 
= (MyDataGrid.CurrentPageIndex > 0); 
InBlock.gif            btnNext.Enabled 
= (MyDataGrid.CurrentPageIndex+1 < MyDataGrid.PageCount);
InBlock.gif            btnLast.Enabled 
= (MyDataGrid.CurrentPageIndex+1 < MyDataGrid.PageCount);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void PagerButtonClick(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string arg = ((LinkButton)sender).CommandArgument.ToString();
InBlock.gif            
switch(arg)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
case "next":
InBlock.gif                    
if (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        MyDataGrid.CurrentPageIndex 
+= 1;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
break;
InBlock.gif                
case "prev":
InBlock.gif                    
if (MyDataGrid.CurrentPageIndex > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        MyDataGrid.CurrentPageIndex 
-= 1;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
break;
InBlock.gif                
case "last":
InBlock.gif                    MyDataGrid.CurrentPageIndex 
= (MyDataGrid.PageCount - 1);
InBlock.gif                    
break;
InBlock.gif                
default:
InBlock.gif                    MyDataGrid.CurrentPageIndex 
= System.Convert.ToInt32(arg);
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif            }

InBlock.gif            BindGrid();
InBlock.gif            ShowStats();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void BindGrid()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            OleDbConnection myConnection 
= cn;
InBlock.gif            DataSet ds  
= new DataSet();
InBlock.gif            OleDbDataAdapter adapter  
= new OleDbDataAdapter("Select Title,pubdate from titles", myConnection);
InBlock.gif            adapter.Fill(ds, 
"Document");
InBlock.gif            MyDataGrid.DataSource 
= ds.Tables["Document"].DefaultView;
InBlock.gif            MyDataGrid.DataBind();
InBlock.gif            ShowStats();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void MyDataGrid_Page(object sender, DataGridPageChangedEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif        
//    int startIndex ;
InBlock.gif        
//    startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize;
InBlock.gif
            MyDataGrid.CurrentPageIndex = e.NewPageIndex;
InBlock.gif            BindGrid();
InBlock.gif            ShowStats();
ExpandedSubBlockEnd.gif        }

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

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.MyDataGrid.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.MyDataGrid_ItemCommand);
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private void MyDataGrid_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (e.CommandName == "Select")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Response.Write(
"Hello");
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/DoNetBird/archive/2005/06/27/181993.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值