键盘敲击者cncxz

  博客园  :: 首页  ::  :: 联系 :: 订阅 订阅  :: 管理

与此控件相关的介绍文章

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

using System;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.ComponentModel.Design;

namespace myControl
{
    
/// <summary>
    
/// 客户端计时器clientTimer控件
    
/// 在线考试系统中卷面计时所用,你可以自由修改
    
/// 丛兴滋(cncxz)    2005-12-3
    
/// </summary>

    [Description("客户端计时器clientTimer")]
    [Designer(
typeof(clientTimerDesigner))]
    [ToolboxData(
"<{0}:clientTimer runat=server></{0}:clientTimer>")]
    
public class clientTimer: System.Web.UI.WebControls.PlaceHolder
    
{

        
public onTimeOutEventHandler onTimeOut;        //超时事件
        private LinkButton myLB;
        
private Label myLabel;

        
"公共属性"


        
public clientTimer()
        
{
            myLB
=new LinkButton();            
            myLB.Click
+=new EventHandler(myLB_Click);
            myLabel
=new Label();
        }


        
private void myLB_Click(object sender, System.EventArgs e){
            
if(onTimeOut!=null){
                onTimeOut();
            }

        }


        
protected override void OnLoad(EventArgs e)
        
{
            
if(this.TimerEnabled)
            
{
                myLB.ID
=this.ClientID+"_LB_TimeOut";
                myLB.Text
="";

                myLabel.ID
=this.ClientID+"_Label_Msg";
                myLabel.Text
="";

                
this.Controls.Add(myLB);
                
this.Controls.Add(myLabel);
            }

            
base.OnLoad(e);
            
        }


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

            }

            
base.Render(writer);
        }


        
private string  strJS(int intCycLength,string strUnits){
            
            
string strFunction=this.ClientID+"_Timer";
            
string strTimeOut=this.ClientID+"_TimeOut";
            
string strPassTime=this.ClientID+"_PassTime";

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

            
else
            
{
                scriptString 
+= @"        document.getElementById("""+this.myLabel.ClientID+@""").innerText=""已用时间:""+"+strPassTime+@"+"""+strUnits+@""";"+"\n";
            }
            
            scriptString 
+= "    }else{\n";
            scriptString 
+= @"        //时间到"+"\n";
            scriptString 
+= @"        document.getElementById("""+this.myLB.ClientID+@""").click();"+"\n";
            scriptString 
+= "    }\n";
            scriptString 
+= @"    window.setTimeout("""+strFunction+@"()"","+intCycLength.ToString()+@");"+"\n";

            scriptString 
+= "}\n";            

            scriptString 
+= @"//-->"+"\n";
            scriptString 
+= @"</script>"+"\n";
            


            
return scriptString;
        }

    }




    
/// <summary>
    
/// 计时单位的类型。
    
/// </summary>

    public enum TimeOutUnitsType:byte
    
{
        
/// <summary>
        
/// 秒。
        
/// </summary>

        Second,
        
/// <summary>
        
/// 分钟。
        
/// </summary>

        Minute,
        
/// <summary>
        
/// 小时。
        
/// </summary>

        Hour
    }


    
public delegate void onTimeOutEventHandler();



    
public class clientTimerDesigner:System.Web.UI.Design.ControlDesigner
    
{
        
private clientTimer CT;
        
public clientTimerDesigner(){

        }

        
public override string GetDesignTimeHtml()
        
{
            CT
=(clientTimer)Component;
            
string str="";
            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');"">";
            str
+=CT.ID+@"</span>";
            
return str;
        }

    }


    

}

posted on 2005-12-03 16:51  cncxz(虫虫)  阅读(2901)  评论(3编辑  收藏  举报