自定义控件Pager

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Caching;

namespace blackflash{
 /// <summary>
 /// Pager 的摘要说明。
 /// </summary>
 [DefaultProperty("Text"),
  ToolboxData("<{0}:Pager runat=server></{0}:Pager>")]
 public class Pager : Control
 {
  private int maxpage;
  private int page;
  private int per;
  private int total;
  private string url;
   
  [Bindable(true),
   Category("Appearance"),
   DefaultValue("")]

  public Pager()
  {
   this.url = "?";
   this.page = 1;
   this.maxpage = 1;
   this.per = 20;
   this.total = 0;  
  }
  public int MaxPage
  {
   get
   {
    return this.maxpage;
   }
   set
   {
    this.maxpage = value;
   }
  }
  public int PageId
  {
   get
   {
    return this.page;
   }
   set
   {
    this.page = value;
   }
  }
  public int Per
  {
   get
   {
    return this.per;
   }
   set
   {
    this.per = value;
   }
  }
  public int Total
  {
   get
   {
    return this.total;
   }
   set
   {
    this.total = value;
   }
  }
  public string Url
  {
   get
   {
    return this.url;
   }
   set
   {
    this.url = value;
   }
  }
  /// <summary>
  /// 将此控件呈现给指定的输出参数。
  /// </summary>
  /// <param name="output"> 要写出到的 HTML 编写器 </param>
  protected override void Render(HtmlTextWriter output)
  {
   output.Write(this.GetContent());
  }
  /// <summary>
  ///分页数据集返回DataTable数据,分页程序
  /// </summary>
  /// <param name="Table">表名 必须有</param>
  /// <param name="Condition">条件比如:issenderdel=0 可以为空</param>
  /// <param name="Compositor">排序 id desc,可以为空</param>
  /// <param name="Mainkey">主键,必须有</param>
  /// <param name="list">列名,可以为空,空为*,所以列</param>
  /// <returns></returns>
  public DataTable GetData(string Table,string Condition,string Compositor,string Mainkey,string list)
  {
   string SqlStr;
   if (list == "")
   {
    list="*";   
   }
   SqlStr="select count(*) from "+Table;
   if (Condition!="")
   {
    SqlStr+=" where "+Condition;
   }
   this.total=Convert.ToInt32(DataAccess.ExecuteScalar(SqlStr));

   if (total%per==0)
   {
    this.maxpage= total/per;
   }
   else
   {
    this.maxpage= total/per+1;
   }
   if (this.page<1)
   {
    this.page = 1 ;
   }
   else if (page>maxpage)
   {
    this.page = maxpage;
   }
   if (this.page<=1)
   {
    SqlStr="select top " + this.per + " "+list+" from "+Table;
    if (Condition !="")
    {
     SqlStr+=" where "+Condition;
    }
    if (Compositor !="")
    {
     SqlStr+=" order by "+Compositor;
    }
   }
   else
   {
    SqlStr="select top " + this.per + " "+list+" from "+Table+"  where "+Mainkey+" not in (select top " + ((page-1)*per) + " "+Mainkey+" from "+Table;
    if (Condition!="")
    {
     SqlStr+=" where "+Condition;
    }
    if (Compositor !="")
    {
     SqlStr+=" order by "+Compositor;
    }
    if (Condition!="")
    {
     SqlStr+=") and "+Condition;
     if (Compositor !="")
     {
      SqlStr+=" order by "+Compositor;
     }
    }
    else
    {
     SqlStr+=")";
     if (Compositor !="")
     {
      SqlStr+=" order by "+Compositor;
     }
    }
   }
   return DataAccess.dataTable(SqlStr);
  }
  public string GetContent()
  {
    StringBuilder sb = new StringBuilder();
    sb.Append("<script language='javascript'>");
    sb.Append("var s = pager('"+this.url+"total="+this.total+"&',"+ this.page +", "+this.maxpage+"," +this.total+ ","+ this.per+", true);");
    sb.Append("document.write(s);");
    sb.Append("</script>");

   return sb.ToString();
  }

 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值