.net 4.0网页按钮点击事件两次触发

时间:2011-06-21 00:12:25

标签: c# asp.net c#-4.0

我有一个4.0网页,点击按钮运行服务器端进程大约需要5-10秒才能运行。我以为我会聪明并且搜索一些代码,这些代码使按钮隐藏并在进程运行时显示动画gif图像。
我得到了这个工作但是开始注意到“文件被另一个进程使用”错误被抛出服务器端(该过程的一部分是删除目录)。我通过调试并开始看到似乎是第二个按钮事件点击同时被触发。事实证明,我添加的用于刷新页面以允许动画gif运行的Js代码似乎是第二次触发服务器点击事件。我不得不调用Js SetTimeOut()方法来运行gif图像,这就是出现问题的原因。 我做了一些搜索尝试找到一个不同的方法来让动画GIF运行,但找不到一个,所以我去了悬崖底部的救护车,并决定添加一个服务器端时间戳到如果会话最后一次设置超过20秒,则只会继续使用我的代码 我用这种方法得到了一些非常不稳定的结果,任何输入都会受到赞赏。

  1. 我有更好的方式让gif运行吗?
  2. 有没有其他人遇到过这个事件两次发射的问题?
  3. 为什么我的锁第一次不能工作?
  4. 任何有关更好方法的建议都表示赞赏,谢谢!

    记录结果:

    第一次通过(没有会话)

    2011-06-21 11:46:14.8968 | DEBUG | FileViewer.copyfiles | Count = 1&锁定=假 2011-06-21 11:46:14.8968 | DEBUG | FileViewer.copyfiles | Count = 2&锁定=真实 2011-06-21 11:46:19.0619 | DEBUG | FileViewer.copyfiles | Count = 1&锁定=假 2011-06-21 11:46:19.0619 | DEBUG | FileViewer.copyfiles | Count = 2&锁定=真实 2011-06-21 11:46:23.1959 | DEBUG | FileViewer.copyfiles | Count = 3&锁定=真实 2011-06-21 11:46:28.8119 | DEBUG | FileViewer.copyfiles | Count = 3&已锁定=正确

    再次跑:

    2011-06-21 11:49:47.7798 | DEBUG | FileViewer.copyfiles | Count = 1&锁定=假 2011-06-21 11:49:47.7798 | DEBUG | FileViewer.copyfiles | Count = 2&锁定=真实 2011-06-21 11:49:55.9697 | DEBUG | FileViewer.copyfiles | Count = 3&锁定=真实 2011-06-21 11:49:59.8697 | DEBUG | FileViewer.copyfiles | Count = 1&锁定=真实 2011-06-21 11:49:59.8697 | DEBUG | FileViewer.copyfiles | Count = 3&已锁定=正确

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PostBackOptions optionsSubmit = new PostBackOptions(btnGo);
            btnGo.OnClientClick = "HideControlOnClick(this);";
            btnGo.OnClientClick += ClientScript.GetPostBackEventReference(optionsSubmit);
        }
    }
    protected void btnGo_Click(object sender, EventArgs e)
    {
        bool locked = true;
    
        if (Session["ClickTime"] == null || (DateTime)Session["ClickTime"] < DateTime.Now.AddSeconds(-20))
        {
            Session["ClickTime"] = DateTime.Now;
            locked = false;
        }
    
        WriteToLog(1, locked);
    
        if (Page.IsValid && !locked)
        {
            locked = true;
    
            WriteToLog(2, locked);
    
            // Do all my processing
        }
    
        WriteToLog(3, locked);
    }
    
    <script language="javascript" type="text/javascript">
    
        function HideControlOnClick(btnGo) 
        {
            // IE uses className for the css property.        
            btnGo.setAttribute('className', 'hide');
    
            document.getElementById('MainContent_imgWait').setAttribute('className', 'show');
    
            setTimeout("UpdateImg('MainContent_imgWait','Images/loading.gif');",50);
        }
        function UpdateImg(ctrl, imgsrc) 
        {
            var img = document.getElementById(ctrl);
            img.src = imgsrc;
        }
    </script>
    

4 个答案:

答案 0 :(得分:0)

我认为最好使用一些Web调试工具来跟踪请求。我建议使用Fiddler

答案 1 :(得分:0)

我怀疑这里的问题可能与动画gif无关,但按钮被按下两次(即两次点击而不是一次注册)。你可以尝试禁用按钮来抑制第二次点击(另一种方法是按钮级别有标志) - 例如,

btnGo.OnClientClick = "return HideControlOnClick(this);";


function HideControlOnClick(btnGo) 
{
   if (btnGo["My_Is_Clicked"]) {
      // already clicked, ignore
      return false;
   }
   btnGo["My_Is_Clicked"] = true;
   ...
   return true;
}

答案 2 :(得分:0)

我通过将所有相关控件放入更新面板解决了这个问题,这似乎允许动画gif在不调用setTimeout的情况下运行(“UpdateImg('MainContent_imgWait','Images / loading.gif') ;”,50); JS方法。现在工作正常,服务器事件只触发一次。

答案 3 :(得分:0)

问题很可能是你正在使用服务器端按钮控件(因为你引用它是服务器端),并且那些通常在点击时发出回发(很难说不知道确切的对象你) '正在使用)。使用HTML输入来激活客户端事件,完成工作,然后从JS代码执行回发(使用适当的ID来捕获服务器端)。

像服务器这样的东西:

/// <summary>
/// Page init event, setup the core of the page.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected override void Page_Init(object sender, EventArgs e)
{
  try
  {
    // See if we're in a postback.
    if (IsPostBack)
    {
      // If we have a target...
      if (Request.Params["__EVENTTARGET"] != null)
      {
        // See what the target is.
        switch (Request.Params["__EVENTTARGET"])
        {
          case "btnGo":
            // Maybe make this a parameterless method rather than an event handler to avoid parameters that you don't need.
            btnGo_Click(null, null);
          break;
        }
      }
    }
  }
}

客户方:

function Go() {
    // Show loading...

    // Call server.
    __doPostBack("btnGo", "My Args");
    }
编辑:作为替代方案,我认为你也可以添加“return false;”在客户端事件之后,例如:

btnGo.OnClientClick =“HideControlOnClick(this); return false;”;

这也应该停止回发。

相关问题