代碼如下:
AutoPage.ascx頁(yè)面
<%@ControlLanguage="c#"AutoEventWireup="false"Codebehind="AutoPage.ascx.cs"
Inherits="album.AutoPage"TargetSchema="//schemas.microsoft.com/intellisense/ie5"%>
<tableborder="0"cellpadding="0"cellspacing="0">
<tr>
<tdvalign="middle"height="30">共<asp:labelid="lb_ItemCount"ForeColor="Red"runat="server">
</asp:label>條記錄 </td>
<tdvalign="middle"height="30"><asp:hyperlinkid="hpl_First"runat="server">金喜正規(guī)買(mǎi)球</asp:hyperlink>
</td>
<tdvalign="middle"height="30"><asp:hyperlinkid="hpl_Prev"runat="server">上頁(yè)</asp:hyperlink>
</td>
<tdvalign="middle"height="30">當(dāng)前<asp:labelid="lb_CurrentPage"runat="server">
</asp:label>頁(yè)/共<asp:labelid="lb_PageCount"runat="server"></asp:label>頁(yè) </td>
<tdvalign="middle"height="30"><asp:hyperlinkid="hpl_Next"runat="server">下頁(yè)</asp:hyperlink>
</td>
<tdvalign="middle"height="30"><asp:hyperlinkid="hpl_Last"runat="server">末頁(yè)</asp:hyperlink>
</td>
<tdvalign="middle"height="30"><asp:textboxid="txb_Page"runat="server"Width="32px"
BorderStyle="Solid"BorderWidth="1px"
BorderColor="Silver"></asp:textbox></td>
<tdvalign="middle"height="30"><asp:ImageButtonid="btn_go"runat="server"
ImageUrl="album_images/go.gif"></asp:ImageButton></td>
<tdvalign="middle"height="30"><asp:labelid="lb_url"runat="server"Visible="False">
</asp:label><asp:Labelid="lb_Params"runat="server"Visible="False"></asp:Label></td>
</tr>
</table>
AutoPage.ascx.cs頁(yè)面
namespacealbum
{
usingSystem;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Web;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.HtmlControls;
usingSystem.Data.SqlClient;
///<summary>
///UC的摘要說(shuō)明。
///</summary>
publicclassAutoPage:System.Web.UI.UserControl
{
protectedSystem.Web.UI.WebControls.HyperLinkhpl_First;
protectedSystem.Web.UI.WebControls.HyperLinkhpl_Prev;
protectedSystem.Web.UI.WebControls.HyperLinkhpl_Next;
protectedSystem.Web.UI.WebControls.Labellb_CurrentPage;
protectedSystem.Web.UI.WebControls.Labellb_PageCount;
protectedSystem.Web.UI.WebControls.HyperLinkhpl_Last;
publicintpagesize;
publicstringPageP;
protectedSystem.Web.UI.WebControls.TextBoxtxb_Page;
protectedSystem.Web.UI.WebControls.Labellb_url;
protectedSystem.Web.UI.WebControls.Labellb_ItemCount;
publicstringurl;
protectedSystem.Web.UI.WebControls.Labellb_Params;
protectedSystem.Web.UI.WebControls.ImageButtonbtn_go;
publicstringParams;
privatevoidPage_Load(objectsender,System.EventArgse)
{
}
publicPagedDataSourcedatabind(DataTabledt)
{
lb_url.Text=url;
lb_Params.Text=Params;
//創(chuàng)建分頁(yè)類(lèi)
PagedDataSourceobjPage=newPagedDataSource();
//設(shè)置數(shù)據(jù)源
objPage.DataSource=dt.DefaultView;
//允許分頁(yè)
objPage.AllowPaging=true;
//設(shè)置每頁(yè)顯示的項(xiàng)數(shù)
objPage.PageSize=pagesize;
//設(shè)置當(dāng)前頁(yè)的索引
intCurPage=1;
try
{
CurPage=Convert.ToInt32(PageP);
if(CurPage<1||CurPage>objPage.PageCount)
{
Response.Redirect(url+"?page=1"+Params);
}
}
catch
{
Response.Redirect(url+"?page=1"+Params);
}
objPage.CurrentPageIndex=CurPage-1;
//顯示狀態(tài)信息
lb_ItemCount.Text=dt.Rows.Count.ToString();
lb_CurrentPage.Text=CurPage.ToString();
lb_PageCount.Text=objPage.PageCount.ToString();
//如果當(dāng)前頁(yè)面不是金喜正規(guī)買(mǎi)球
if(!objPage.IsFirstPage)
{
hpl_Prev.NavigateUrl=url+"?Page="+Convert.ToString(CurPage-1)+Params;
hpl_First.NavigateUrl=url+"?Page=1"+Params;
}
//如果當(dāng)前頁(yè)面不是最后一頁(yè)
if(!objPage.IsLastPage)
{
hpl_Next.NavigateUrl=url+"?Page="+Convert.ToString(CurPage+1)+Params;
hpl_Last.NavigateUrl=url+"?Page="+objPage.PageCount.ToString()+Params;
}
returnobjPage;
}
#regionWeb窗體設(shè)計(jì)器生成的代碼
overrideprotectedvoidOnInit(EventArgse)
{
//
//CODEGEN:該調(diào)用是ASP.NETWeb窗體設(shè)計(jì)器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
///<summary>
///設(shè)計(jì)器支持所需的方法-不要使用代碼編輯器
///修改此方法的內(nèi)容。
///</summary>
privatevoidInitializeComponent()
{
this.btn_go.Click+=newSystem.Web.UI.ImageClickEventHandler(this.btn_go_Click);
this.Load+=newSystem.EventHandler(this.Page_Load);
}
#endregion
privatevoidbtn_go_Click(objectsender,System.Web.UI.ImageClickEventArgse)
{
Response.Redirect(lb_url.Text+"?Page="+txb_Page.Text+lb_Params.Text);
}
}
}
調(diào)用的時(shí)候需要設(shè)置幾個(gè)參數(shù)pagesize(每頁(yè)顯示數(shù)據(jù)個(gè)數(shù)),PageP(傳遞的分頁(yè)參數(shù)),ParmP(其他的Request.QureyString參數(shù)),url(頁(yè)面地址)
綁定的時(shí)候只需要把控件的DataSource=AutoPage1.databind(DataTable變量)
|