title: Intercept button click on page load
body: <p>I need to write a code that can intercept a click of some button (asp button) than execute some code, and if a method return true, call the original click.</p>
  
  <p>So the points are:</p>
  
  <p>1- I don´t know how to save the original click.</p>
  
  <p>2- Identify the button that was clicked.</p>
  
  <p>Ex:</p>
  
  <pre><code>    protected void Page_Load(object sender, EventArgs e)
      {
          var b = getButtonThatWasClicked();
  
          var originalClick = b.Click;
  
          if(callSomeMethod(b))
              originalClick(null,null);
      }
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>Ok managed to get the button who made the click doing this...Now i need to prevent the original Click to get called. The method bellow didn't worked. Even overriding the original click to a new Handler the old Handler got called and executed. I thing ASP.NET read it and make something like a call stack of events to get called.Even if the handler change the old still in the stack.</p>
  
  <pre><code>    public void ButtonsTestMethod()
      {
          var listOfButtons = listaDeBotes.Where(b =&gt; b.CommandName != "");
  
          foreach (var button in listOfButtons)
          {
              if (Request.Form[button.UniqueID] != null)
              {
                  var buttonFromRequest = Request.Form[button.UniqueID];
                  if (buttonFromRequest == null)
                      continue;
  
                  if (button.CommandName != "xxx")
                  { 
                      //validate things
                      //if(TemPermissao(Tela,GetAcaoDoBotao(botao.CommandName)))
                      //if(!canexecuteSomething())
                      button.Click += new EventHandler(defaultClick);   
                  }
              }
          }
      }
  
      void defaultClick(object sender, EventArgs e)
      {
          //show error
      }
  
      protected void Page_Load(object sender, EventArgs e)
      {
          //other code
          ButtonsTestMethod();
      }
  </code></pre>
  
  <p>I don´t know if its possible but would appreciate some help =/</p>
  
  <p>Thanks.</p>