网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > ASP.NET技巧
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.asp.net应用程序级别跟踪.
.修改asp.net2.0内置成员管理中的.
.动态控制Page页的Head信息.
.ASP.NET创建Web服务之使用事务.
.FCKeditor 2.3 在ASP.NET中文件上.
.通过系统配置来提高ASP.NET应用程.
.ASP.NET如何存取SQL Server数据库.
.关于ASP.NET调用JavaScript的实现.
.权限树中Checkbox的操作[Asp.Net.
.收藏一段小的.net下的验证码片段.
.Asp.net自动返回上次请求页面.
.在ASP.NET中从SQL Server检索图片.
.Login控件在UpdatePanel内当验证.
.ASP.NET 2.0高级数据处理之冲突检.
.ASP.NET中如何防范SQL注入式攻击.
.通过系统配置来提高ASP.NET应用程.
.asp.net1.1 开发专用模板类.
.ASP.NET实现匿名访问控制.
.那数组存入application,再利用循.
.MySQL与ASP.NET配合更强大.

Asp.net url分页的用户控件

发表日期:2005-6-19


出处:snooker_li的专栏

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

代码如下:

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>条记录&nbsp;</td>
  <td valign="middle" height="30"><asp:hyperlink id="hpl_First" runat="server">首页</asp:hyperlink>&nbsp;</td>
  <td valign="middle" height="30"><asp:hyperlink id="hpl_Prev" runat="server">上页</asp:hyperlink>&nbsp;</td>
  <td valign="middle" height="30">当前<asp:label id="lb_CurrentPage" runat="server"></asp:label>页/共<asp:label id="lb_PageCount" runat="server"></asp:label>页&nbsp;</td>
  <td valign="middle" height="30"><asp:hyperlink id="hpl_Next" runat="server">下页</asp:hyperlink>&nbsp;</td>
  <td valign="middle" height="30"><asp:hyperlink id="hpl_Last" runat="server">末页</asp:hyperlink>&nbsp;</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变量)

上一篇:把.NET程序部署到没有安装.NET Framwork的机器上 人气:12214
下一篇:ASP.NET 2.0中DataTable小兵变大将 人气:9507
浏览全部Asp.net的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐