在DataGrid中实现分页

    用过ASP的朋友也许对起分页感到头大,但在ASP。NET中其自动分页功能便让您能得心应手。这是前台代码:

后台代码如下:
None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Data.SqlClient;
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
using  System.Configuration;
None.gif
None.gif
namespace  mynew
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// WebForm1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class WebForm1 : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected System.Web.UI.WebControls.LinkButton LinkButton1;
InBlock.gif        
protected System.Web.UI.WebControls.LinkButton LinkButton2;
InBlock.gif        
protected System.Web.UI.WebControls.LinkButton LinkButton3;
InBlock.gif        
protected System.Web.UI.WebControls.LinkButton LinkButton4;
InBlock.gif        
protected System.Web.UI.WebControls.Label lblCurrentIndex;
InBlock.gif        
protected System.Web.UI.WebControls.Label lblPageCount;        
InBlock.gif        
protected System.Web.UI.WebControls.DataGrid MyDataGrid;
InBlock.gif    
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlConnection conn
=new SqlConnection(ConfigurationSettings.AppSettings["dsn"]);
InBlock.gif            conn.Open();
InBlock.gif            SqlDataAdapter myAdapter
=new SqlDataAdapter();
InBlock.gif            myAdapter.SelectCommand
=new SqlCommand("select CategoryID,CategoryName from Categories",conn);
InBlock.gif
//            myAdapter.SelectCommand.CommandType=CommandType.StoredProcedure;
InBlock.gif
            DataSet ds=new DataSet();
InBlock.gif            myAdapter.Fill(ds,
"Categories");
InBlock.gif            MyDataGrid.DataSource
=ds;
InBlock.gif            MyDataGrid.DataBind();
InBlock.gif            lblCurrentIndex.Text
=""+((Int32)MyDataGrid.CurrentPageIndex+1)+"";
InBlock.gif            lblPageCount.Text
="/共"+MyDataGrid.PageCount+"";
InBlock.gif            conn.Close();
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
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.LinkButton1.Click += new System.EventHandler(this.PagerButton_Click);
InBlock.gif            
this.LinkButton2.Click += new System.EventHandler(this.PagerButton_Click);
InBlock.gif            
this.LinkButton3.Click += new System.EventHandler(this.PagerButton_Click);
InBlock.gif            
this.LinkButton4.Click += new System.EventHandler(this.PagerButton_Click);
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif
InBlock.gif        
private void PagerButton_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string arg=((LinkButton)sender).CommandArgument; 
InBlock.gif            
//获取LinkButton的参数值
InBlock.gif
            switch(arg)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
case ("next"):
InBlock.gif                    
if(MyDataGrid.CurrentPageIndex<(MyDataGrid.PageCount-1))
InBlock.gif                        MyDataGrid.CurrentPageIndex
++;
InBlock.gif                    
break;
InBlock.gif                
case("prev"):
InBlock.gif                    
if(MyDataGrid.CurrentPageIndex>0)
InBlock.gif                        MyDataGrid.CurrentPageIndex
--;
InBlock.gif                    
break;
InBlock.gif                
case("first"):
InBlock.gif                    MyDataGrid.CurrentPageIndex
=0;
InBlock.gif                    
break;
InBlock.gif                
case("last"):
InBlock.gif                    MyDataGrid.CurrentPageIndex
=MyDataGrid.PageCount-1;
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif            }

InBlock.gif            MyDataGrid.DataBind();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

就怎么如次  easy 一个简单的分页便实现了。

转载于:https://www.cnblogs.com/long/archive/2004/12/19/79190.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值