用戶自定義控件的應用。
2024-07-21 02:24:17
供稿:網友
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 |總頁數