打印

[NET精华教程] .net教程:用户自定义控件的应用

.net教程:用户自定义控件的应用

  
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 |总页数




TOP

返回顶部
AYBlue

Processed in 0.041549 second(s), 7 queries.

当前时区 GMT+8, 现在时间是 2009-1-8 14:20 京ICP备06054220号

清除 Cookies - 联系我们 - 163K.com - Archiver - WAP