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

通用分页控件(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()
Codehighlighter1_2998_3235_Closed_Image
分享到:
评论

你可能感兴趣的:(JavaScript,UI,Web,asp,Go)