ASP.NET: OnServerClick event handler not called if using onclick

后端 未结 8 1672
囚心锁ツ
囚心锁ツ 2020-12-05 21:35

I have a peculiar problem here and I can\'t by my life figure out what the solution is. Note that the following code is not dynamically created, but just immediatel

相关标签:
8条回答
  • 2020-12-05 22:09

    The accepted answer is not perfect. If you do not use type="button", the web page will do postback even you have clicked cancel. The correct and easiest way is to take advantage of short-circuit evaluation and do this hack: replace ; with && !, like below.

    <button runat="server" id="btnSubmit"
            OnServerClick="btnSubmit_Click" onclick="return confirm('Sure?') && !">
    

    The output will look like this:

    <button id="btnSubmit"
            onclick="return confirm('Sure?') && ! __doPostBack('btnSubmit','')">
    

    It gives correct return value because true && !undefined will return true and undefined will be evaluated and false && !undefined will return false and undefined will NOT be evaluated which is exactly what we want.

    0 讨论(0)
  • 2020-12-05 22:10

    Front Site

      <button  id="submit1" runat="server"  
                  onclick="if(confirm('Sure?')) { } else{ return false} ;"    
                  onserverclick="submit_ServerClick"  >save</button>
    

    Back Site

    protected void submit_ServerClick(object sender, EventArgs e)
    {
    }
    
    0 讨论(0)
提交回复
热议问题