using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CustomWebControl.PageNavigation
{
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
public class PageNavigation : WebControl, IPostBackDataHandler, IPostBackEventHandler
{
public const string PAGE_CURRENT = "pageCurrent";
public const string PAGE_SUM = "pageSum";
public const string CSS_CLASS = "cssClass";
public const string SEARCH_STATE = "searchState";
[Bindable(true), Category("数据"), DefaultValue(1), Description("当前页码")]
public int PageCurrent
{
get
{
int pageCurrent = null == ViewState[PAGE_CURRENT] ? 1 : (int)ViewState[PAGE_CURRENT];
return pageCurrent;
}
set
{
ViewState[PAGE_CURRENT] = value;
}
}
[Bindable(true), Category("数据"), DefaultValue(0), Description("总页数")]
public int PageSum
{
get
{
int pageSum = null == ViewState[PAGE_SUM] ? 0 : (int)ViewState[PAGE_SUM];
return pageSum;
}
set
{
ViewState[PAGE_SUM] = value;
}
}
[Bindable(true), Category("外观"), DefaultValue(""), Description("Css Class名称")]
public override string CssClass
{
get
{
string str = null == ViewState[CSS_CLASS] ? string.Empty : ViewState[CSS_CLASS].ToString();
return str;
}
set
{
ViewState[CSS_CLASS] = value;
}
}
//事件键定义
private static readonly object _event = new object();
/// <summary>
/// 当前页改变事件
/// </summary>
[Category("事件"), Description("当前页码改变时触发此事件")]
public event EventHandler ChangePageCurrent
{
add
{
Events.AddHandler(_event, value);
}
remove
{
Events.RemoveHandler(_event, value);
}
}
/// <summary>
/// 当前页改变的事件方法
/// </summary>
/// <param name="e"></param>
protected virtual void OnChangePageCurrent(EventArgs e)
{
EventHandler handler = (EventHandler)Events[_event];
if (handler != null)
{
handler(this, e);
}
}
protected override void RenderContents(HtmlTextWriter output)
{
if (this.PageSum != 0 && this.PageCurrent > this.PageSum)
this.PageCurrent = this.PageSum;
if (this.PageSum != 0 && this.PageCurrent < 1)
this.PageCurrent = 1;
output.AddAttribute(HtmlTextWriterAttribute.Width, "100%");
output.AddAttribute(HtmlTextWriterAttribute.Height, "30");
output.AddAttribute(HtmlTextWriterAttribute.Border, "0");
output.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
output.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
output.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass);
output.RenderBeginTag(HtmlTextWriterTag.Table);
output.RenderBeginTag(HtmlTextWriterTag.Tr);
output.RenderBeginTag(HtmlTextWriterTag.Td);
output.AddAttribute(HtmlTextWriterAttribute.Align, "right");
output.RenderBeginTag(HtmlTextWriterTag.Div);
output.AddStyleAttribute(HtmlTextWriterStyle.Cursor, "pointer");
output.AddStyleAttribute(HtmlTextWriterStyle.TextDecoration, "underline");
if (this.PageCurrent == 1 || this.PageSum == 0)
output.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
else
output.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackEventReference(this, "First"));
output.RenderBeginTag(HtmlTextWriterTag.Span);
output.Write("首 页");
output.RenderEndTag();
output.Write(" ");
output.AddStyleAttribute(HtmlTextWriterStyle.Cursor, "pointer");
output.AddStyleAttribute(HtmlTextWriterStyle.TextDecoration, "underline");
if (this.PageCurrent == 1 || this.PageSum == 0)
output.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
else
output.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackEventReference(this, "Previous"));
output.RenderBeginTag(HtmlTextWriterTag.Span);
output.Write("上 页");
output.RenderEndTag();
output.Write(" ");
output.AddStyleAttribute(HtmlTextWriterStyle.Cursor, "pointer");
output.AddStyleAttribute(HtmlTextWriterStyle.TextDecoration, "underline");
if (this.PageCurrent == this.PageSum || this.PageSum == 0)
output.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
else
output.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackEventReference(this, "Next"));
output.RenderBeginTag(HtmlTextWriterTag.Span);
output.Write("下 页");
output.RenderEndTag();
output.Write(" ");
output.AddStyleAttribute(HtmlTextWriterStyle.Cursor, "pointer");
output.AddStyleAttribute(HtmlTextWriterStyle.TextDecoration, "underline");
if (this.PageCurrent == this.PageSum || this.PageSum == 0)
output.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
else
output.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackEventReference(this, "Last"));
output.RenderBeginTag(HtmlTextWriterTag.Span);
output.Write("尾 页");
output.RenderEndTag();
output.Write(" ");
output.RenderBeginTag(HtmlTextWriterTag.Span);
output.Write("页 码:" + (this.PageSum==0?"0":this.PageCurrent.ToString()) + "/" + this.PageSum.ToString() + " 转 到:");
output.RenderEndTag();
output.AddAttribute(HtmlTextWriterAttribute.Onchange,Page.ClientScript.GetPostBackEventReference(this, "Go"));
output.AddAttribute(HtmlTextWriterAttribute.Id, this.UniqueID);
output.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
output.RenderBeginTag(HtmlTextWriterTag.Select);
for (int i = 1; i <= this.PageSum; i++)
{
if (i != this.PageCurrent)
output.Write("<option value=\"" + i.ToString() + "\">" + i.ToString() + "</option>");
else
output.Write("<option value=\"" + i.ToString() + "\" selected=\"selected\">" + i.ToString() + "</option>");
}
output.AddAttribute(HtmlTextWriterAttribute.Onchange, Page.ClientScript.GetPostBackEventReference(this, "pageCurrent"));
output.RenderEndTag();
output.Write(" ");
output.RenderEndTag();
output.RenderEndTag();
output.RenderEndTag();
output.RenderEndTag();
}
protected override void Render(HtmlTextWriter writer)
{
if (Page != null)
{
Page.VerifyRenderingInServerForm(this);
}
base.Render(writer);
}
void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
{
switch (eventArgument)
{
case "First":
if (this.PageCurrent == 1)
return;
this.PageCurrent = 1;
OnChangePageCurrent(EventArgs.Empty);
break;
case "Previous":
if (this.PageCurrent > 1)
this.PageCurrent = this.PageCurrent - 1;
else
return;
OnChangePageCurrent(EventArgs.Empty);
break;
case "Next":
if (this.PageCurrent < this.PageSum)
this.PageCurrent = this.PageCurrent + 1;
else
return;
OnChangePageCurrent(EventArgs.Empty);
break;
case "Last":
if (this.PageCurrent == this.PageSum)
return;
this.PageCurrent = this.PageSum;
OnChangePageCurrent(EventArgs.Empty);
break;
case "Go":
OnChangePageCurrent(EventArgs.Empty);
break;
}
}
public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
{
int pageCurrentValue = PageCurrent;
int pageCurrentPostedValue = int.Parse(postCollection[postDataKey]);
if (pageCurrentValue != pageCurrentPostedValue)
{
this.PageCurrent = pageCurrentPostedValue;
return true;
}
return false;
}
public void RaisePostDataChangedEvent()
{
}
}
}