[assembly: TagPrefix("WebPages", "Pages")] //XXX为自定义控件的名称空间,YYY为你所想要得名字
空间前缀自定义
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 = "";
protected int CurrentPage
{
set
{
if (Regex.IsMatch(Convert.ToString(value), "^\\d+$"))
{
this._CuttentPage = Convert.ToInt32(value);
}
else
{
this._CuttentPage = 0;
}
}
get
{
return this._CuttentPage;
}
}
protected int NumCount
{
set
{
if (Regex.IsMatch(Convert.ToString(value), "^\\d+$"))
{
this._NumCount = Convert.ToInt32(value);
}
else
{
this._NumCount = 0;
}
}
get
{
return this._NumCount;
}
}
protected int PageSize
{
set
{
if (Regex.IsMatch(Convert.ToString(value), "^\\d+$"))
{
this._PageSize = Convert.ToInt32(value);
}
else
{
this._PageSize = 1;
}
}
get
{
return this._PageSize;
}
}
//地址
protected string Url
{
set
{
_Url = value;
}
get
{
if (string.IsNullOrEmpty(_Url))
{
try
{
_Url = this.Parent.Page.Request.Url.ToString();
}
catch
{
_Url = "";
}
}
return this._Url;
}
}
[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 getUrl(string url)
{
MatchCollection mats = Regex.Matches(url, "(?<first>[\\w]*)=(?<two>[\\S\\s]*[\u4e00-\u9fa5]+[\\S\\s]*)", 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 = url.IndexOf("page=") >= 0 ? (Regex.Replace(url, "page=([^&]*)", "page={0}")) : (url.IndexOf("?") >= 0 ? (url + "&page={0}") : (url + "?page={0}"));
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)));
}
}
}
posted on 2009-02-27 16:29
sanmao 阅读(71)
评论(0) 编辑 收藏