首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 笔记本电脑 | 北大青鸟 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Dotnet频道
中国IT教育
Google
首页 ASP.NET  C#  XML/WebService ADO.NET VC.NET VB.NET .NET 资讯动态 专题 RSS订阅 讨论 下载
您现在的位置: 中国IT实验室 >> Dotnet >> ASP.NET >> 正文

Asp.net url分页的用户控件

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

    代码如下:

    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变量)

【责编:yuan】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·开源软件测试工具学习专题
 ·JSP Web开发 入门基础到高手进阶教程…
 ·JavaFX—是Java桌面的新希望么?
 ·安全至上 .NET开发安全策略…
 ·测试用例设计之道-测试用例学习专题
 ·面向Java开发人员的Scala指南
 ·Java设计模式之实例详解
 ·Oracle数据库11g 面向DBA和开发人员的重要新特性…
 ·桌面应用软件编程 J2SE技术详解…
 ·我“炫”我精彩-------WPF开发教程
 今日更新
 社区讨论
 博客论点
 频道精选
 Dotnet频道相关导航