控件
asp.net中的 用户自定义控件 特点 1:实现服用;2:方便创建(相对与组件)。 以下为一个分页导航条的sample, 接见于Webdiyer,相信很多人已经如雷贯耳了,我也不多介绍。本问只是简单的 练习之作,没有什么深奥的算法和架构。
----behindCode---------------------------------------------------------------------------------------------------------------
namespace GallonKit
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
/// <summary>
/// PageBar 的摘要说明。
/// </summary>
public delegate void PageLocalBtn(uint index);
public class PageBar : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.LinkButton LinkButton1;
protected System.Web.UI.WebControls.LinkButton btn_fistPage;
protected System.Web.UI.WebControls.LinkButton btn_lastPage;
protected System.Web.UI.WebControls.LinkButton btn_prePage;
protected System.Web.UI.WebControls.LinkButton btn_nextPage;
protected System.Web.UI.WebControls.Label lb_PageCount;
protected System.Web.UI.WebControls.TextBox tb_pageIndex;
protected System.Web.UI.WebControls.LinkButton btn_local;
public PageLocalBtn LocalBtnClick;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
private bool IsUint(string strInt)
{
try
{
uint.Parse(strInt);
}
catch(Exception e)
{
return false;
}
return true;
}
public uint pageIndex
{
get{
if (this.IsUint(this.tb_pageIndex.Text.Trim()))
return uint.Parse(this.tb_pageIndex.Text.Trim());
return 0;
}
set{
if (this.IsUint(value.ToString()))
this.tb_pageIndex.Text = value.ToString();
else
throw new System.Exception("页数范围不正确!");
}
}
public uint pageCount
{
get
{
object obj = ViewState["PageCount__"];
return (obj == null)?0:(uint)obj;
}
set
{
if (this.IsUint(value.ToString()))
{
this.lb_PageCount.Text = value.ToString();
ViewState["PageCount__"] = value;
}
else
throw new System.Exception("页数范围不正确!");
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btn_fistPage.Click += new System.EventHandler(this.btn_fistPage_Click);
this.btn_prePage.Click += new System.EventHandler(this.btn_prePage_Click);
this.btn_nextPage.Click += new System.EventHandler(this.btn_nextPage_Click);
this.btn_lastPage.Click += new System.EventHandler(this.btn_lastPage_Click);
this.btn_local.Click += new System.EventHandler(this.btn_local_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void EnableAllBtn()
{
this.btn_fistPage.Enabled = true;
this.btn_lastPage.Enabled = true;
this.btn_prePage.Enabled = true;
this.btn_nextPage.Enabled = true;
}
private void ChangeBtnStatus(char BtnIndex)
{
switch(BtnIndex)
{
case 'F':
this.btn_fistPage.Enabled = false;
this.btn_prePage.Enabled = false;
break;
case 'L':
this.btn_nextPage.Enabled = false;
this.btn_lastPage.Enabled = false;
break;
case 'P':
this.btn_prePage.Enabled = false;
break;
case 'N':
this.btn_nextPage.Enabled = false;
break;
}
}
private void btn_lastPage_Click(object sender, System.EventArgs e)
{
EnableAllBtn();
if( LocalBtnClick != null)
{
LocalBtnClick(this.pageCount);
}
this.ChangeBtnStatus('L');
}
private void btn_fistPage_Click(object sender, System.EventArgs e)
{
EnableAllBtn();
if( LocalBtnClick != null)
{
LocalBtnClick(1);
}
this.ChangeBtnStatus('F');
}
private void btn_prePage_Click(object sender, System.EventArgs e)
{
EnableAllBtn();
if (LocalBtnClick == null) return;
if (this.pageIndex>1)
this.pageIndex--;
else
{
this.ChangeBtnStatus('P');
this.ChangeBtnStatus('F');
}
this.LocalBtnClick(this.pageIndex);
}
private void btn_nextPage_Click(object sender, System.EventArgs e)
{
EnableAllBtn();
if (LocalBtnClick == null) return;
if (this.pageIndex<this.pageCount)
this.pageIndex++;
else
{
this.ChangeBtnStatus('N');
this.ChangeBtnStatus('L');
}
this.LocalBtnClick(this.pageIndex);
}
private void btn_local_Click(object sender, System.EventArgs e)
{
EnableAllBtn();
if (this.pageIndex <=1)
{
this.tb_pageIndex.Text = "1";
this.ChangeBtnStatus('F');
}
else
{
if(this.pageIndex >= this.pageCount )
{
this.pageIndex = this.pageCount;
this.ChangeBtnStatus('L');
}
}
if( LocalBtnClick != null)
LocalBtnClick(this.pageIndex);
}
}
}
====================aspx文件=====================================================
| 首页 | 前页 | 后页 | 末页 |1 Go |总页数