当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > Asp.net url分页的用户控件

ASP.NET
为T-SQL添加intellisense功能
SQL Server 2005安装过程中出现错误的解决办法
SQL Server 2005 RTM 安装错误 :The SQL Server System Configuration Checker cannot be executed due to
有关于JSON的一些资料
不能忽略c#中的using和as操作符的用处
JavaScript系列之―同步还是异步?
获取远程网页的内容之一(downmoon原创)
获取远程网页的内容之二(downmoon原创)
ASP.Net中防止刷新自动触发事件的解决方案
asp.net下用js实现鼠标移至小图,自动显示相应大图
Asp.Net 和 AJAX.Net 的区别
提交页面的定位--scrollIntoView的用法
利用AJAX与数据岛实现无刷新绑定
asp.net下判断用户什么时候离开,以什么方式离开
DataSet 添加数据集、行、列、主键和外键等操作示例
读写xml所有节点个人小结 和 读取xml节点的数据总结
收藏的asp.net文件上传类源码
asp.net下GDI+的一些常用应用(水印,文字,圆角处理)技巧
一个可以让.net程序在非WIN平台上运行的软件Mono
使用ASP.NET 2.0 CSS 控件适配器生成CSS友好的HTML输出

ASP.NET 中的 Asp.net url分页的用户控件


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 134 ::
收藏到网摘: n/a

出处:snooker_li的专栏

最近做一个相册程序频繁的需要分页,所以就想写一个用户控件出来。

代码如下:

AutoPage.ascx页面

<%@ControlLanguage="c#"AutoEventWireup="false"Codebehind="AutoPage.ascx.cs"Inherits="album.AutoPage"TargetSchema="http://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>条记录&nbsp;</td>
<tdvalign="middle"height="30"><asp:hyperlinkid="hpl_First"runat="server">首页</asp:hyperlink>&nbsp;</td>
<tdvalign="middle"height="30"><asp:hyperlinkid="hpl_Prev"runat="server">上页</asp:hyperlink>&nbsp;</td>
<tdvalign="middle"height="30">当前<asp:labelid="lb_CurrentPage"runat="server"></asp:label>页/共<asp:labelid="lb_PageCount"runat="server"></asp:label>页&nbsp;</td>
<tdvalign="middle"height="30"><asp:hyperlinkid="hpl_Next"runat="server">下页</asp:hyperlink>&nbsp;</td>
<tdvalign="middle"height="30"><asp:hyperlinkid="hpl_Last"runat="server">末页</asp:hyperlink>&nbsp;</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页面

namespacealbum
{
usingSystem;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Web;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.HtmlControls;
usingSystem.Data.SqlClient;

///<summary>
///UC的摘要说明。
///</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;
//创建分页类
PagedDataSourceobjPage=newPagedDataSource();
//设置数据源
objPage.DataSource=dt.DefaultView;
//允许分页
objPage.AllowPaging=true;
//设置每页显示的项数
objPage.PageSize=pagesize;
//设置当前页的索引
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;
//显示状态信息
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;
}
returnobjPage;
}


#regionWeb窗体设计器生成的代码
overrideprotectedvoidOnInit(EventArgse)
{
//
//CODEGEN:该调用是ASP.NETWeb窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

///<summary>
///设计器支持所需的方法-不要使用代码编辑器
///修改此方法的内容。
///</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);
}


}
}

调用的时候需要设置几个参数pagesize(每页显示数据个数),PageP(传递的分页参数),ParmP(其他的Request.QureyString参数),url(页面地址)

绑定的时候只需要把控件的DataSource=AutoPage1.databind(DataTable变量)

"