最近做一个相册程序频繁的需要分页,所以就想写一个用户控件出来。
代码如下:
autopage.ascx页面
<%@ control language="c#" autoeventwireup="false" codebehind="autopage.ascx.cs" inherits="album.autopage" targetschema="http://schemas.microsoft.com/intellisense/ie5" %>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td valign="middle" height="30">共<asp:label id="lb_itemcount" forecolor="red" runat="server"></asp:label>条记录 </td>
<td valign="middle" height="30"><asp:hyperlink id="hpl_first" runat="server">首页</asp:hyperlink> </td>
<td valign="middle" height="30"><asp:hyperlink id="hpl_prev" runat="server">上页</asp:hyperlink> </td>
<td valign="middle" height="30">当前<asp:label id="lb_currentpage" runat="server"></asp:label>页/共<asp:label id="lb_pagecount" runat="server"></asp:label>页 </td>
<td valign="middle" height="30"><asp:hyperlink id="hpl_next" runat="server">下页</asp:hyperlink> </td>
<td valign="middle" height="30"><asp:hyperlink id="hpl_last" runat="server">末页</asp:hyperlink> </td>
<td valign="middle" height="30"><asp:textbox id="txb_page" runat="server" width="32px" borderstyle="solid" borderwidth="1px"
bordercolor="silver"></asp:textbox></td>
<td valign="middle" height="30"><asp:imagebutton id="btn_go" runat="server" imageurl="album_images/go.gif"></asp:imagebutton></td>
<td valign="middle" height="30"><asp:label id="lb_url" runat="server" visible="false"></asp:label><asp:label id="lb_params" runat="server" visible="false"></asp:label></td>
</tr>
</table>
autopage.ascx.cs页面
namespace album
{
using system;
using system.data;
using system.drawing;
using system.web;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
using system.data.sqlclient;
/// <summary>
/// uc 的摘要说明。
/// </summary>
public class autopage : system.web.ui.usercontrol
{
protected system.web.ui.webcontrols.hyperlink hpl_first;
protected system.web.ui.webcontrols.hyperlink hpl_prev;
protected system.web.ui.webcontrols.hyperlink hpl_next;
protected system.web.ui.webcontrols.label lb_currentpage;
protected system.web.ui.webcontrols.label lb_pagecount;
protected system.web.ui.webcontrols.hyperlink hpl_last;
public int pagesize;
public string pagep;
protected system.web.ui.webcontrols.textbox txb_page;
protected system.web.ui.webcontrols.label lb_url;
protected system.web.ui.webcontrols.label lb_itemcount;
public string url;
protected system.web.ui.webcontrols.label lb_params;
protected system.web.ui.webcontrols.imagebutton btn_go;
public string params;
private void page_load(object sender, system.eventargs e)
{
}
public pageddatasource databind(datatable dt)
{
lb_url.text = url;
lb_params.text = params;
//创建分页类
pageddatasource objpage = new pageddatasource();
//设置数据源
objpage.datasource = dt.defaultview;
//允许分页
objpage.allowpaging = true;
//设置每页显示的项数
objpage.pagesize = pagesize;
//设置当前页的索引
int curpage=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;
//显示状态信息
lb_itemcount.text = dt.rows.count.tostring();
lb_currentpage.text = curpage.tostring();
lb_pagecount.text =objpage.pagecount.tostring();
//如果当前页面不是首页
if (!objpage.isfirstpage)
{
hpl_prev.navigateurl=url + "?page=" + convert.tostring(curpage-1)+params;
hpl_first.navigateurl=url + "?page=1"+params;
}
//如果当前页面不是最后一页
if (!objpage.islastpage)
{
hpl_next.navigateurl=url+ "?page=" + convert.tostring(curpage+1)+params;
hpl_last.navigateurl=url + "?page=" +objpage.pagecount.tostring()+params;
}
return objpage;
}
#region web 窗体设计器生成的代码
override protected void oninit(eventargs e)
{
//
// codegen: 该调用是 asp.net web 窗体设计器所必需的。
//
initializecomponent();
base.oninit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void initializecomponent()
{
this.btn_go.click += new system.web.ui.imageclickeventhandler(this.btn_go_click);
this.load += new system.eventhandler(this.page_load);
}
#endregion
private void btn_go_click(object sender, system.web.ui.imageclickeventargs e)
{
response.redirect(lb_url.text+"?page="+txb_page.text+lb_params.text);
}
}
}
调用的时候需要设置几个参数pagesize(每页显示数据个数),pagep(传递的分页参数),parmp(其他的request.qureystring参数),url(页面地址)
绑定的时候只需要把控件的datasource=autopage1.databind(datatable变量)
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器