客户端计时器控件(clientTimer)的c#源码

与此控件相关的介绍文章

http://cncxz.cnblogs.com/archive/2005/12/03/289959.html

None.gif using  System;
None.gif
using  System.IO;
None.gif
using  System.Web;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.ComponentModel;
None.gif
using  System.ComponentModel.Design;
None.gif
None.gif
namespace  myControl
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 客户端计时器clientTimer控件
InBlock.gif    
/// 在线考试系统中卷面计时所用,你可以自由修改
InBlock.gif    
/// 丛兴滋(cncxz)    2005-12-3
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    [Description("客户端计时器clientTimer")]
InBlock.gif    [Designer(
typeof(clientTimerDesigner))]
InBlock.gif    [ToolboxData(
"<{0}:clientTimer runat=server></{0}:clientTimer>")]
InBlock.gif    
public class clientTimer: System.Web.UI.WebControls.PlaceHolder
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif        
public onTimeOutEventHandler onTimeOut;        //超时事件
InBlock.gif
        private LinkButton myLB;
InBlock.gif        
private Label myLabel;
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
"公共属性"#region "公共属性"
InBlock.gif
InBlock.gif        [Browsable(
true),Category("计时相关"),DefaultValue(TimeOutUnitsType.Minute),Description("计时单位,有秒、分钟、小时三种,默认为分钟。")]
InBlock.gif        
public TimeOutUnitsType TimeOutUnits
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
object obj=ViewState["TimeOutUnits"];
InBlock.gif                
return (obj==null)?TimeOutUnitsType.Minute:(TimeOutUnitsType)obj;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ViewState[
"TimeOutUnits"]=value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        [Browsable(
true),Category("计时相关"),DefaultValue(30),Description("计时超时时间(单位与TimeOutUnits属性一致)。")]
InBlock.gif        
public int TimeOutLength
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
object obj=ViewState["TimeOutLength"];
InBlock.gif                
return (obj==null)?30:int.Parse(obj.ToString());
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ViewState[
"TimeOutLength"]=value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif
InBlock.gif        [Browsable(
true),Category("计时相关"),DefaultValue(0),Description("已用去的时间(单位与TimeOutUnits属性一致)。")]
InBlock.gif        
public int PassTimeLength
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
object obj=ViewState["PassTimeLength"];
InBlock.gif                
return (obj==null)?0:int.Parse(obj.ToString());
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ViewState[
"PassTimeLength"]=value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        [Browsable(
true),Category("行为"),DefaultValue(false),Description("是否以倒计时的方式显示友好界面,是则显示还剩多少时间,否则显示用了多少时间。")]
InBlock.gif        
public bool CountDown
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
object obj=ViewState["CountDown"];
InBlock.gif                
return (obj==null)?false:(bool)obj;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ViewState[
"CountDown"]=value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Browsable(
true),Category("行为"),DefaultValue(true),Description("是否启用计时器")]
InBlock.gif        
public bool TimerEnabled
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
object obj=ViewState["TimerEnabled"];
InBlock.gif                
return (obj==null)?true:(bool)obj;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ViewState[
"TimerEnabled"]=value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif
InBlock.gif        
public clientTimer()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            myLB
=new LinkButton();            
InBlock.gif            myLB.Click
+=new EventHandler(myLB_Click);
InBlock.gif            myLabel
=new Label();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
private void myLB_Click(object sender, System.EventArgs e)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if(onTimeOut!=null)dot.gif{
InBlock.gif                onTimeOut();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void OnLoad(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(this.TimerEnabled)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                myLB.ID
=this.ClientID+"_LB_TimeOut";
InBlock.gif                myLB.Text
="";
InBlock.gif
InBlock.gif                myLabel.ID
=this.ClientID+"_Label_Msg";
InBlock.gif                myLabel.Text
="";
InBlock.gif
InBlock.gif                
this.Controls.Add(myLB);
InBlock.gif                
this.Controls.Add(myLabel);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.OnLoad(e);
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void Render(HtmlTextWriter writer) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(this.TimerEnabled)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
switch(this.TimeOutUnits)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
case TimeOutUnitsType.Second:
InBlock.gif                        writer.Write(
this.strJS(1000," 秒"));
InBlock.gif                        
break;
InBlock.gif                    
case TimeOutUnitsType.Minute:
InBlock.gif                        writer.Write(
this.strJS(60000," 分钟"));
InBlock.gif                        
break;
InBlock.gif                    
case TimeOutUnitsType.Hour:
InBlock.gif                        writer.Write(
this.strJS(3600000," 小时"));
InBlock.gif                        
break;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.Render(writer);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
private string  strJS(int intCycLength,string strUnits)dot.gif{
InBlock.gif            
InBlock.gif            
string strFunction=this.ClientID+"_Timer";
InBlock.gif            
string strTimeOut=this.ClientID+"_TimeOut";
InBlock.gif            
string strPassTime=this.ClientID+"_PassTime";
InBlock.gif
InBlock.gif            
string scriptString ="\n";
InBlock.gif            scriptString 
+= @"<script language=""JavaScript"">"+"\n";
InBlock.gif            scriptString 
+= @"    <!--"+"\n";
InBlock.gif            scriptString 
+= "var "+strTimeOut+"="+this.TimeOutLength.ToString()+"; \n";
InBlock.gif            scriptString 
+= "var "+strPassTime+"="+this.PassTimeLength.ToString()+";\n";
InBlock.gif            scriptString 
+= @"        window.attachEvent(""onload"", "+strFunction+");"+"\n";
InBlock.gif            scriptString 
+="function "+strFunction+"() {\n";
InBlock.gif            scriptString 
+= "    if("+strPassTime+"<"+strTimeOut+"){\n";
InBlock.gif            scriptString 
+= @"        //未超时"+"\n";
InBlock.gif            scriptString 
+= "        "+strPassTime+"+=1;\n";
InBlock.gif            
if(this.CountDown)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                scriptString 
+= "        var myNum="+strTimeOut+"-"+strPassTime+";\n";
InBlock.gif                scriptString 
+= @"        document.getElementById("""+this.myLabel.ClientID+@""").innerText=""剩余时间:""+myNum+"""+strUnits+@""";"+"\n";
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                scriptString 
+= @"        document.getElementById("""+this.myLabel.ClientID+@""").innerText=""已用时间:""+"+strPassTime+@"+"""+strUnits+@""";"+"\n";
ExpandedSubBlockEnd.gif            }
            
InBlock.gif            scriptString 
+= "    }else{\n";
InBlock.gif            scriptString 
+= @"        //时间到"+"\n";
InBlock.gif            scriptString 
+= @"        document.getElementById("""+this.myLB.ClientID+@""").click();"+"\n";
InBlock.gif            scriptString 
+= "    }\n";
InBlock.gif            scriptString 
+= @"    window.setTimeout("""+strFunction+@"()"","+intCycLength.ToString()+@");"+"\n";
InBlock.gif
InBlock.gif            scriptString 
+= "}\n";            
InBlock.gif
InBlock.gif            scriptString 
+= @"//-->"+"\n";
InBlock.gif            scriptString 
+= @"</script>"+"\n";
InBlock.gif            
InBlock.gif
InBlock.gif
InBlock.gif            
return scriptString;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 计时单位的类型。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public enum TimeOutUnitsType:byte
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 秒。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        Second,
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 分钟。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        Minute,
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 小时。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        Hour
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public delegate void onTimeOutEventHandler();
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif    
public class clientTimerDesigner:System.Web.UI.Design.ControlDesigner
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private clientTimer CT;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public clientTimerDesigner()dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public override string GetDesignTimeHtml()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            CT
=(clientTimer)Component;
InBlock.gif            
string str="";
InBlock.gif            str
+=@"<span style=""height:20px;padding:2px 10px 2px 10px;border-left:1px solid #fafafa;border-top:1px solid #fafafa;border-bottom:1px solid #d0d0d0;border-right:1px solid #d0d0d0;FILTER: progid:DXImageTransform.Microsoft.Gradient(startColorStr='#f5f5f5', endColorStr='#e5e5e5', gradientType='0');"">";
InBlock.gif            str
+=CT.ID+@"</span>";
InBlock.gif            
return str;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
posted on 2005-12-03 16:51  cncxz(虫虫) 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/cncxz/archive/2005/12/03/289991.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值