WEBTIMER控件研究的心得:丢开书本做一个WebTimer

先说说几个教训:
1,小心命名,别跟系统已经存在的命名起冲突,比如,我一时疏忽将控件的命名空间设成Control,编译通过后,运行总是不通过,查了半天,才发现这个小问题,再比如,NAMEspace别和类名相同,否则,写起来很是不方便
下面是WEBTIMER的全部代码加了注解的

None.gif using  System;
None.gif
using  System.Web.UI;
None.gif
using  System.Text;
None.gif
using  System.Web;
None.gif
using  System.Data;
None.gif
using  System.ComponentModel;
None.gif
None.gif
namespace  MyControl
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// WebTimer 的摘要说明。
InBlock.gif    
/// </summary>
InBlock.gif    
///Designer属性指定一个类作为设计时类,该类必须派生于ControlDesigner类,并且实现GetDesignHtml方法
InBlock.gif    
///Designer性性是System.ComponentModel空间的类
InBlock.gif    
///WebTimer继承了IPostBackEventHandler是因为任何要利用POSTBACK机制来进行事件处理的控件都必须
ExpandedSubBlockEnd.gif    
///继承该接口并实现RaisePostbackEvent方法

InBlock.gif    [Designer(typeof(MyControl.WebTimerDesigner))]
InBlock.gif    
public class WebTimer:System.Web.UI.Control,System.Web.UI.IPostBackEventHandler
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
成员变量定义#region 成员变量定义
InBlock.gif            
private int _interval=5000;//计时器种子数
InBlock.gif
            private bool _enable=true;//是否允许此计时器
InBlock.gif
            private static  object TimerKey=new object();//TimerKey是做什么用的呢,它是用来做Events的KEY对象的,而且它是静态的,所以,对于类的所有实例,将只耗费一个变量的代价
ExpandedSubBlockEnd.gif
        #endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
属性定义#region 属性定义
InBlock.gif        [Browsable(
true),Bindable(true),DefaultValue(5000),Description("计时器使用的种子数")]
InBlock.gif        
public int Interval
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{_interval=value;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return(_interval);}
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Browsable(
true),Bindable(true),DefaultValue(true),Description("是否允许计时器")]
InBlock.gif        
public bool Enable
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return _enable;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{_enable=true;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Browsable(
true),Bindable(true),DefaultValue(true),Description("是否输出控件")]
InBlock.gif        
public override bool Visible
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{return base.Visible;}
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{base.Visible = value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
事件定义#region 事件定义
InBlock.gif        
public event EventHandler Timer
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            add 
dot.gif{Events.AddHandler(TimerKey,value);}
ExpandedSubBlockStart.gifContractedSubBlock.gif            remove
dot.gif{Events.RemoveHandler(TimerKey,value);}
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
方法定义#region 方法定义
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 这个私有方法是用来在引发POSTBACK事件后,调用事件处理的关键,它再调用相应的事件处理程序
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private  void ProcessTimer(string eventArgument)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            EventHandler TimerHandler
=(EventHandler)Events[TimerKey];
InBlock.gif            
if(TimerHandler!=null)
InBlock.gif                TimerHandler(
this,EventArgs.Empty);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string BuildJavascript()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            StringBuilder JsBuilder
=new StringBuilder();
ExpandedSubBlockStart.gifContractedSubBlock.gif            JsBuilder.AppendFormat(
"<script language='javascript'>setTimeout(\"dot.gif{0}\",{1});</script>",new object[]dot.gif{Page.GetPostBackEventReference(this),Interval});
InBlock.gif            
return(JsBuilder.ToString());
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
IPostBackEventHandler 成员#region IPostBackEventHandler 成员
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 每个要使用POSTBACK机制并且想在POSTBACK后进行适当处理的控件
InBlock.gif        
/// 必须实现IPostBackEventHandler的RaisePostBackEvent方法
InBlock.gif        
/// 并在此方法中调用或间接调用事件处理程序
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="eventArgument"></param>

InBlock.gif        public void RaisePostBackEvent(string eventArgument)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// TODO:  添加 WebTimer.RaisePostBackEvent 实现
InBlock.gif
            ProcessTimer("");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
OnRender方法用来输出HTML#region OnRender方法用来输出HTML
InBlock.gif        
protected override void Render(HtmlTextWriter writer)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(Page!=null)
InBlock.gif                Page.VerifyRenderingInServerForm(
this);
InBlock.gif            
if(_enable)
InBlock.gif                writer.Write(BuildJavascript());
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
OnPreRender方法用来产生HTML#region OnPreRender方法用来产生HTML
InBlock.gif        
protected override void OnPreRender(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.OnPreRender (e);
InBlock.gif            Page.GetPostBackEventReference(
this,"");
InBlock.gif            
//发现这一句可以不加,如果见了的话呢,DOPOSTBACK将会被绘制在前面,否则,绘制在后面
ExpandedSubBlockEnd.gif
        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif        
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

相应的Designer
None.gif using  System;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.Design;
None.gif
using  System.Text;
None.gif
namespace  MyControl
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// WebTimerDesigner 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class WebTimerDesigner:System.Web.UI.Design.ControlDesigner
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public override string GetDesignTimeHtml()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            StringBuilder HtmlBuilder
=new StringBuilder();
InBlock.gif            Control TimerControl
=(Control)this.Component;
InBlock.gif            
if(TimerControl is MyControl.WebTimer)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                HtmlBuilder.AppendFormat(
"<span id=\"dot.gif{0}\" style=\"border:1px solid #666666;font-size:9pt;font-weight:bold\" >{1}</span>",new object[]dot.gif{TimerControl.ClientID,"WebTimer"});
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return(HtmlBuilder.ToString());
ExpandedSubBlockEnd.gif        }
        
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

调用页面的HTML
ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="TimerDemo.WebForm1"  %>
ExpandedBlockStart.gifContractedBlock.gif
<% dot.gif @ Register TagPrefix="cc1" Namespace="MyControl" Assembly="MyControl.WebTimer"  %>
None.gif
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  >
None.gif
< HTML >
None.gif    
< HEAD >
None.gif        
< title > WebForm1 </ title >
None.gif        
< meta  name ="GENERATOR"  Content ="Microsoft Visual Studio .NET 7.1" >
None.gif        
< meta  name ="CODE_LANGUAGE"  Content ="C#" >
None.gif        
< meta  name ="vs_defaultClientScript"  content ="JavaScript" >
None.gif        
< meta  name ="vs_targetSchema"  content ="http://schemas.microsoft.com/intellisense/ie5" >
None.gif    
</ HEAD >
None.gif    
< body  MS_POSITIONING ="GridLayout" >
None.gif        
< form  id ="Form1"  method ="post"  runat ="server" >
None.gif                
< cc1:WebTimer  id ="WebTimer1"  runat ="server" ></ cc1:WebTimer >
None.gif        
</ form >
None.gif    
</ body >
None.gif
</ HTML >
None.gif
相应的代码
None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
None.gif
namespace  TimerDemo
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// WebForm1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class WebForm1 : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected MyControl.WebTimer WebTimer1;
InBlock.gif    
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 在此处放置用户代码以初始化页面
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.WebTimer1.Timer += new System.EventHandler(this.WebTimer1_Timer);
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private void WebTimer1_Timer(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Response.Write(DateTime.Now);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/Heroman/archive/2004/11/29/70614.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值