Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 11th, 2012  |  syntax: None  |  size: 1.55 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. .net 4.0 webpage button click event firing twice
  2. protected void Page_Load(object sender, EventArgs e)
  3. {
  4.     if (!IsPostBack)
  5.     {
  6.         PostBackOptions optionsSubmit = new PostBackOptions(btnGo);
  7.         btnGo.OnClientClick = "HideControlOnClick(this);";
  8.         btnGo.OnClientClick += ClientScript.GetPostBackEventReference(optionsSubmit);
  9.     }
  10. }
  11. protected void btnGo_Click(object sender, EventArgs e)
  12. {
  13.     bool locked = true;
  14.  
  15.     if (Session["ClickTime"] == null || (DateTime)Session["ClickTime"] < DateTime.Now.AddSeconds(-20))
  16.     {
  17.         Session["ClickTime"] = DateTime.Now;
  18.         locked = false;
  19.     }
  20.  
  21.     WriteToLog(1, locked);
  22.  
  23.     if (Page.IsValid && !locked)
  24.     {
  25.         locked = true;
  26.  
  27.         WriteToLog(2, locked);
  28.  
  29.         // Do all my processing
  30.     }
  31.  
  32.     WriteToLog(3, locked);
  33. }
  34.  
  35. <script language="javascript" type="text/javascript">
  36.  
  37.     function HideControlOnClick(btnGo)
  38.     {
  39.         // IE uses className for the css property.        
  40.         btnGo.setAttribute('className', 'hide');
  41.  
  42.         document.getElementById('MainContent_imgWait').setAttribute('className', 'show');
  43.  
  44.         setTimeout("UpdateImg('MainContent_imgWait','Images/loading.gif');",50);
  45.     }
  46.     function UpdateImg(ctrl, imgsrc)
  47.     {
  48.         var img = document.getElementById(ctrl);
  49.         img.src = imgsrc;
  50.     }
  51. </script>
  52.        
  53. btnGo.OnClientClick = "return HideControlOnClick(this);";
  54.  
  55.  
  56. function HideControlOnClick(btnGo)
  57. {
  58.    if (btnGo["My_Is_Clicked"]) {
  59.       // already clicked, ignore
  60.       return false;
  61.    }
  62.    btnGo["My_Is_Clicked"] = true;
  63.    ...
  64.    return true;
  65. }