C#委托在自定义控件中的使用

本文介绍了一种在ASP.NET中使用DataGrid进行数据分页的方法。通过设置DataGrid属性和绑定事件,实现了数据的分页显示,并提供了跳转到首页、上一页、下一页及最后一页的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主要事件如下:

  /// </summary>
  /// <param name="adg">DataGrid控件名</param>
  /// <param name="NewBind">数据绑定事件委托</param>
  /// <param name="iPageSize">每页显示数</param>
  public void SetTarget(DataGrid adg,BindDataDelegate NewBind,int iPageSize) {
   _dg=adg;
   BindData=new BindDataDelegate(NewBind);

   this.First.Click+= new System.Web.UI.ImageClickEventHandler(this.NavigationButton_Click);
   this.Previous.Click += new System.Web.UI.ImageClickEventHandler(this.NavigationButton_Click);
   this.Next.Click += new System.Web.UI.ImageClickEventHandler(this.NavigationButton_Click);
   this.Lastly.Click += new System.Web.UI.ImageClickEventHandler(this.NavigationButton_Click);
   _dg.DataBinding += new System.EventHandler(this.BeforeDataBinding);

   _dg.AllowPaging = true;
   _dg.AllowCustomPaging = false;
   _dg.PagerStyle.Visible = false;
   pagesize=iPageSize;
   _dg.PageSize = pagesize;
   _dg.PagerStyle.Mode = PagerMode.NumericPages;
   _dg.PagerStyle.HorizontalAlign = HorizontalAlign.Right;   
  }

 

 

 private void NavigationButton_Click(object sender, System.Web.UI.ImageClickEventArgs e) {
   string strCommandName=((ImageButton)sender).ID;//取得事件的对象名

   switch(strCommandName) {
    case "First":
     _dg.CurrentPageIndex=0;//跳转到首页
     PageOperate();
     break;
    case "Previous":
     _dg.CurrentPageIndex=Math.Max(_dg.CurrentPageIndex-1,0);//跳转到上一页
     PageOperate();
     break;
    case "Next":
     _dg.CurrentPageIndex=Math.Min(_dg.CurrentPageIndex+1,_dg.PageCount-1);//跳转到下一页
     PageOperate();
     break;
    case "Lastly":
     _dg.CurrentPageIndex=_dg.PageCount-1;//跳转到最后一页
     PageOperate();
     break;
   }  
   BindData();//绑定数据
  }

 

 

  //分页按钮事件
  private void NavigationButton_Click(object sender, System.Web.UI.ImageClickEventArgs e) {
   string strCommandName=((ImageButton)sender).ID;//取得事件的对象名

   switch(strCommandName) {
    case "First":
     _dg.CurrentPageIndex=0;//跳转到首页
     PageOperate();
     break;
    case "Previous":
     _dg.CurrentPageIndex=Math.Max(_dg.CurrentPageIndex-1,0);//跳转到上一页
     PageOperate();
     break;
    case "Next":
     _dg.CurrentPageIndex=Math.Min(_dg.CurrentPageIndex+1,_dg.PageCount-1);//跳转到下一页
     PageOperate();
     break;
    case "Lastly":
     _dg.CurrentPageIndex=_dg.PageCount-1;//跳转到最后一页
     PageOperate();
     break;
   }  
   BindData();//绑定数据
  }

转载于:https://www.cnblogs.com/linpengfeixgu/articles/1459949.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值