using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Web.UI;
// 有关程序集的常规信息通过下列属性集
// 控制。更改这些属性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("WebPages")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("WebPages")]
[assembly: AssemblyCopyright("版权所有 (C) 2009")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// 将 ComVisible 设置为 false 使此程序集中的类型
// 对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型,
// 则将该类型上的 ComVisible 属性设置为 true。
[assembly: ComVisible(false)]
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("6b878ccc-3322-4f3d-a8c6-ce080520f0bd")]
[assembly: TagPrefix("WebPages", "WebPages")] //XXX为自定义控件的名称空间,YYY为你所想要得名字
// 程序集的版本信息由下面四个值组成:
//
// 主版本
// 次版本
// 内部版本号
// 修订号
//
// 可以指定所有这些值,也可以使用“修订号”和“内部版本号”的默认值,
// 方法是按如下所示使用“*”:
[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyFileVersion("1.0.0.0")]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
namespace WebPages
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:Pages runat=\"server\"></{0}:Pages>")]
public class Pages : WebControl
{
private int _CuttentPage;
private int _NumCount;
private int _PageSize = 1;
private string _Url = "";
private string _RewriteFormat = "";
private string _RewriteRegex = "";
public int CurrentPage
{
set
{
if (Regex.IsMatch(Convert.ToString(value), "^\\d+$"))
{
this._CuttentPage = Convert.ToInt32(value);
}
else
{
this._CuttentPage = 0;
}
}
get
{
return this._CuttentPage;
}
}
public int NumCount
{
set
{
if (Regex.IsMatch(Convert.ToString(value), "^\\d+$"))
{
this._NumCount = Convert.ToInt32(value);
}
else
{
this._NumCount = 0;
}
}
get
{
return this._NumCount;
}
}
public int PageSize
{
set
{
if (Regex.IsMatch(Convert.ToString(value), "^\\d+$"))
{
this._PageSize = Convert.ToInt32(value);
}
else
{
this._PageSize = 1;
}
}
get
{
return this._PageSize;
}
}
//地址
public string Url
{
set
{
_Url = value;
}
get
{
if (string.IsNullOrEmpty(_Url))
{
try
{
_Url = this.Parent.Page.Request.Url.ToString();
}
catch
{
_Url = "";
}
}
return this._Url;
}
}
public string RewriteFormat
{
set { this._RewriteFormat = value; }
get { return this._RewriteFormat; }
}
public string RewriteRegex
{
set { _RewriteRegex = value; }
get { return this._RewriteRegex; }
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? String.Empty : s);
}
set
{
ViewState["Text"] = value;
}
}
protected string GetRightRegexUrl(string url)
{
string[,] tempStrArr = new string[12,2];
tempStrArr[0, 0] = "\\";
tempStrArr[0, 1] = "\\\\u005C";
tempStrArr[1, 0] = "$";
tempStrArr[1, 1] = "\\\\u0024";
tempStrArr[2, 0] = "^";
tempStrArr[2, 1] = "\\\\u005E";
tempStrArr[3, 0] = "{";
tempStrArr[3, 1] = "\\\\u007B";
tempStrArr[4, 0] = "[";
tempStrArr[4, 1] = "\\\\u005B";
tempStrArr[5, 0] = "(";
tempStrArr[5, 1] = "\\\\u0028";
tempStrArr[6, 0] = "|";
tempStrArr[6, 1] = "\\\\u007C";
tempStrArr[7, 0] = ")";
tempStrArr[7, 1] = "\\\\u0029";
tempStrArr[8, 0] = "*";
tempStrArr[8, 1] = "\\\\u002A";
tempStrArr[9, 0] = "+";
tempStrArr[9, 1] = "\\\\u002B";
tempStrArr[10, 0] = "?";
tempStrArr[10, 1] = "\\\\\\\\u003F";
tempStrArr[11, 0] = ".";
tempStrArr[11, 1] = "\\\\\\\\u002E";
for (int i = 0; i < 12; i++)
{
url = url.Replace(tempStrArr[i,0],tempStrArr[i,1]);
}
return url;
}
protected string getUrl(string url)
{
MatchCollection mats = Regex.Matches(url, "(?<first>[\\w]*)=(?<two>[^&]*[\u4e00-\u9fa5]+[^&]*)", RegexOptions.IgnoreCase | RegexOptions.Multiline);
foreach (Match mat in mats)
{
url = url.Replace(mat.Groups["first"].Value + "=" + mat.Groups["two"].Value, mat.Groups["first"].Value + "=" + HttpUtility.UrlEncodeUnicode(mat.Groups["two"].Value.Trim()));
}
return url;
}
/// <summary>
/// 根据当前的页数显示的当前页的底部分页
/// </summary>
/// <param name="current"></param>
/// <param name="size"></param>
/// <param name="zong"></param>
/// <returns></returns>
protected string GetPage(int current, int size, int zong)
{
string url = this.Url;
url = getUrl(url);
string urltemp ="";
urltemp = url.IndexOf("page=") >= 0 ? (Regex.Replace(url, "page=([^&]*)", "page={0}",RegexOptions.Multiline|RegexOptions.Singleline)) : (url.IndexOf("?") >= 0 ? (url + "&page={0}") : (url + "?page={0}"));
if(this.RewriteFormat.Length>0)
{
Regex reg = new Regex(RewriteRegex, RegexOptions.IgnoreCase);
if (reg.IsMatch(urltemp))
{
urltemp = reg.Replace(urltemp, RewriteFormat);
}
}
int pagecount = zong % size == 0 ? (zong / size) : (zong / size + 1);
int num = current / 10;
System.Text.StringBuilder sball = new System.Text.StringBuilder("");
int xian = current / 10 + 1;
int before = current % 10 == 0 ? (current / 10) : (current / 10);
sball.AppendFormat(" <a href='" + string.Format(urltemp, current - 1 < 1 ? ("1") : ((current - 1).ToString())) + "'>上一页</a> ");
for (int i = ((before * 10 - 5) > 0 ? (before * 10 - 5) : (1)); i <= (before * 10 == 0 ? (1) : (before * 10)); i++)
{
if ((before * 10 - 5) < 0)
continue;
sball.AppendFormat(" <a href='" + string.Format(urltemp, (i + 1).ToString()) + "'>{0}</a>", (i + 1).ToString());
sball.Append(" ");
}
for (int i = ((xian - 1) * 10 > pagecount ? (pagecount) : ((xian - 1) * 10)); i < (xian * 10 < pagecount ? (xian * 10) : (pagecount)); i++)
{
if (i == 0)
sball.AppendFormat("<a href='" + string.Format(urltemp, (i + 1).ToString()) + "'>").Append((i + 1)).Append("</a>");
else
sball.Append(" <a href='" + string.Format(urltemp, (i + 1).ToString()) + "'>").Append((i + 1)).Append("</a>");
}
sball.Append(" <a href='" + string.Format(urltemp, (current + 1 > pagecount ? (pagecount.ToString()) : ((current + 1).ToString()))) + "'>下一页</a>");
sball.Append(" 共").Append(zong.ToString()).Append("条 当前第").Append((current).ToString()).Append("页").Append("共").Append(pagecount).Append("页");
return sball.ToString();
}
protected override void RenderContents(HtmlTextWriter output)
{
string tempData = "<table border=\"1\" width=\"100%\" height=\"30px\" style=\"margin:0px\"><tr><td style=\"width: 89%\">{0}</td><td style=\"width: 30%\"><input type=\"text\" id=\"Pages1_TbPageGo\" onKeyUp=\"this.value=this.value.replace(/[^\\d]+/,'')\" style=\"width: 33px\"/><input id=\"BtGo\" style=\"width: 34px\" type=\"button\" value=\"GO\" onClick=\"document.location.href=document.location.href.indexOf('page=')>=0?(document.location.href.replace(/page=([^&]*)/,'page='+document.getElementById('Pages1_TbPageGo').value)):(document.location.href.indexOf('?')>=0?(document.location.href+'&page='+document.getElementById('Pages1_TbPageGo').value):(document.location.href+'?page='+document.getElementById('Pages1_TbPageGo').value))\"/></td></tr></table>";
// output.Write(Text);
//<%=GetPage(this.CurrentPage, this.PageSize, this.NumCount, this.Request.Url.ToString())%>
output.Write(string.Format(tempData, GetPage(CurrentPage,PageSize,NumCount)));
}
}
}