通用分页控件(DataGrid,DataList,Repeater都可以用它来分页)

1.建立用户控件Pager.ascx

1.1 html

< script language ="javascript" >
functioncallButtonEvent()
{
varkeycode=window.event.keyCode;
if(keycode==13)
{
if(check()==true)
{
event.cancelBubble
=true;
event.returnValue
=false;
document.getElementById('
<%=btnGo.ClientID%>').click();
}

}

}

functioncheck()
{
varcount=parseInt(document.getElementById('<%=lblTotal.ClientID%>').outerText);
vartxt=document.getElementById('<%=txtCurrentPage.ClientID%>').value;
varcur=parseInt(txt);
if((cur|NaN)==0)
{
alert('Inputpagemustformatasinteger.');
event.cancelPostBack
=true;
returnfalse;
}

if(cur>count||cur<1)
{
alert('Inputpagenooutofrange.');
event.cancelPostBack
=true;
returnfalse;
}

}

</ script >
< TABLE ID ="Table1" CELLSPACING ="0" CELLPADDING ="0" WIDTH ="100%" BORDER ="0" >
< colgroup >
< col width ="400" >
< col width ="50" >
< col width ="50" >
< col width ="40" >
< col width ="20" >
< col width ="40" >
< col width ="40" >
< col width ="50" >
< col width ="70" >
</ colgroup >
< TR align ="right" >
< td ></ td >
< TD >< asp:LinkButton id ="btnFirstPage" runat ="server" CommandArgument ="First" > 第一页 </ asp:LinkButton ></ TD >
< TD >< asp:LinkButton id ="btnPrevPage" runat ="server" CommandArgument ="Prev" > 上一页 </ asp:LinkButton ></ TD >
< TD >< ASP:TEXTBOX ID ="txtCurrentPage" RUNAT ="server" MAXLENGTH ="3" Width ="40" > 0 </ ASP:TEXTBOX ></ TD >
< TD >< ASP:LABEL ID ="labOf" RUNAT ="server" > of </ ASP:LABEL ></ TD >
< TD >< ASP:LABEL ID ="lblTotal" RUNAT ="server" > 0 </ ASP:LABEL ></ TD >
< TD >< ASP:BUTTON ID ="btnGo" RUNAT ="server" TEXT ="转到" COMMANDARGUMENT ="Go" ToolTip ="转到" ></ ASP:BUTTON ></ TD >
< TD >< asp:LinkButton id ="btnNextPage" runat ="server" CommandArgument ="Next" > 下一页 </ asp:LinkButton ></ TD >
< TD >< asp:LinkButton id ="btnLastPage" runat ="server" CommandArgument ="Last" > 最后一页 </ asp:LinkButton ></ TD >
</ TR >
</ TABLE >

1.2 cs代码

public class Pager:System.Web.UI.UserControl
{
protectedSystem.Web.UI.WebControls.LabellblTotal;
protectedSystem.Web.UI.WebControls.LabellabOf;
protectedSystem.Web.UI.WebControls.TextBoxtxtCurrentPage;
protectedSystem.Web.UI.WebControls.ButtonbtnGo;
protectedSystem.Web.UI.WebControls.LinkButtonbtnFirstPage;
protectedSystem.Web.UI.WebControls.LinkButtonbtnPrevPage;
protectedSystem.Web.UI.WebControls.LinkButtonbtnNextPage;
protectedSystem.Web.UI.WebControls.LinkButtonbtnLastPage;
intsize=10;//可以在web.config中配置
publiceventSystem.EventHandlerNavigationClick;

privatevoidPage_Load(objectsender,System.EventArgse)
{
this.txtCurrentPage.Attributes.Add("onkeypress","callButtonEvent();");
this.btnGo.Attributes.Add("onclick","check();");
if(!this.IsPostBack)
{
SetStyle();
SetEnable();
}

}


WebFormDesignergeneratedcode#regionWebFormDesignergeneratedcode
overrideprotectedvoidOnInit(EventArgse)
{
//
//CODEGEN:ThiscallisrequiredbytheASP.NETWebFormDesigner.
//
InitializeComponent();
base.OnInit(e);
this.btnFirstPage.Click+=newSystem.EventHandler(this.btnGo_Click);
this.btnPrevPage.Click+=newSystem.EventHandler(this.btnGo_Click);
this.btnNextPage.Click+=newSystem.EventHandler(this.btnGo_Click);
this.btnLastPage.Click+=newSystem.EventHandler(this.btnGo_Click);
this.btnGo.Click+=newSystem.EventHandler(this.btnGo_Click);
}


/**////<summary>
///RequiredmethodforDesignersupport-donotmodify
///thecontentsofthismethodwiththecodeeditor.
///</summary>

privatevoidInitializeComponent()
{
this.Load+=newSystem.EventHandler(this.Page_Load);

}

#endregion


btnGo_Click#regionbtnGo_Click
privatevoidbtnGo_Click(objectsender,System.EventArgse)
{
LinkButtonlinkbtn
=senderasLinkButton;
if(null==linkbtn)//button
{
Buttonbtn
=senderasButton;
if(null==btn)
{
return;
}

else
{
intselPage=-1;
try
{
selPage
=Int32.Parse(txtCurrentPage.Text);
}

catch
{
selPage
=-1;
}

if(selPage>0&&selPage<=PageCount)
{
ViewState[
"CurrentPageIndex"]=selPage;
}

else
{
return;
}

}

}

else//linkbutton
{
switch(linkbtn.CommandArgument.Trim())
{
case"First":
ViewState[
"CurrentPageIndex"]=1;
break;
case"Prev":
ViewState[
"CurrentPageIndex"]=(CurrentPageIndex>1)?CurrentPageIndex-1:1;
break;
case"Next":
ViewState[
"CurrentPageIndex"]=(PageCount>CurrentPageIndex)?CurrentPageIndex+1:PageCount;
break;
case"Last":
ViewState[
"CurrentPageIndex"]=PageCount;
break;
default:
break;
}

}

SetEnable();
//设置显示样式
if(NavigationClick!=null)//调用事件
{
NavigationClick(sender,e);
}

}

#endregion


SetStyle#regionSetStyle
privatevoidSetStyle()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值