C#委托在自定义控件中的使用
主要事件如下:
/// </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();//绑定数据
}