我写了一个倒计时的用户控件,是用脚本计算,然后我在一个页面上使用了多个这个用户控件,但是这几个用户控件的脚本互相影响了,要怎么做才能让这几个脚本不做影响呢?

解决方案 »

  1.   

    影响的只是变量
    将变量保存在控件dom中呢
      

  2.   

    脚本的变量和函数名称不能相同,你要跟COntrolID关联起来
      

  3.   

    你是不在用户控件中写了类似这样的js语句:var timeOutEvent = setTimeout(你的执行代码, 时间间隔);
    //这样的话肯定会影响的,因为变量名称重复了
      

  4.   

    很简单的例子
    WebControl1.ascx代码<%@ Control Language="C#" ClassName="WebControl1" %>
    <script runat="server">
      public int CountDown {set;get;} 
    </script>
    <script type="text/javascript">
      var CountDown<%=this.ClientID %> = <%=CountDown %>;
      function cd<%=this.ClientID %>()
      {
        document.getElementById('<%=p.ClientID %>').innerHTML = CountDown<%=this.ClientID %>;
        CountDown<%=this.ClientID %>--;
      }
      setInterval('cd<%=this.ClientID %>()',1000);
    </script>
    <asp:Panel ID="p" runat="server"></asp:Panel>测试代码
    <%@ Page Language="C#" %><%@ Register Src="WebControl1.ascx" TagName="WebControl1" TagPrefix="uc1" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
      <title></title>
    </head>
    <body>
      <form id="form1" runat="server">
      <uc1:WebControl1 ID="WebControl1" runat="server" CountDown="200" />
      <uc1:WebControl1 ID="WebControl2" runat="server" CountDown="20" />
      <uc1:WebControl1 ID="WebControl3" runat="server" CountDown="500" />
      </form>
    </body>
    </html>