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

后端 未结 8 1671
囚心锁ツ
囚心锁ツ 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 21:44

    I had more success with

    <asp:Button ID="btnSubmit" runat="server" Text="Save" UseSubmitBehaviour="false"
    OnClick="btnSubmit_Click" OnClientClick="if (!confirm('Sure?')) return" />
    
    0 讨论(0)
  • 2020-12-05 21:50

    < button> is an HtmlButton control where OnServerClick is the only server-side click event. OnClick is unrecognized by ASP.NET so it gets passed to the client intact in case you want some client-side javascript code to execute before the postback.

    < asp:button> is a WebControl which accomplishes the same thing with the OnClick server-side event and OnClientClick for client-side.

    I think you're getting the behavior you see because your confirm('sure?') returns false which stops the postback mechanism. That would explain why it works when you don't have onclick in place.

    0 讨论(0)
  • 2020-12-05 21:54

    It sounds like the onclick event isn't bubbling through.

    You should just be able to use

    OnClientClick="return confirm('Sure?');
    

    I don't think the other onClick should be necessary.

    Edit:

    This method would require you to hook your function to the OnClick event manually.


    I am making up attributes this morning so in a futile effort to redeem myself-

    Another method would be to insert javascript to catch the event. Something like..

    $("form").submit(function() {
    
            var resp = confirm("Save & Submit?");
            if (resp) {
                serializeStats();
                return true;
            }
            else {
                return false;
            }
    
        });
    

    I do recall there being a better inline way to do this though. Caffeine time now.

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

    How about chaging button's type to submit, it works well :

    <button type="submit" runat="server" id="btnSubmit"
      OnServerClick="btnSubmit_Click" onclick="return confirm('Sure?');">
        Submit
    </button>
    
    0 讨论(0)
  • 2020-12-05 22:06

    Try this:

    <button type="button" runat="server" id="btnSubmit"
    OnServerClick="btnSubmit_Click" onclick="if(!confirm('Sure?')) return;">
    Submit
    </button>
    
    0 讨论(0)
  • 2020-12-05 22:08

    If you look at the source code generated you will see the following:

    onclick="return confirm('Sure?'); __doPostBack('btnSubmit','')"
    

    so what is happening is the _doPostBack is never called. The hacky way to do what you're looking for is the following:

    <button type="button" runat="server" id="btnSubmit"
      OnServerClick="btnSubmit_Click" onclick="if (confirm('Sure?')) ">
    

    The real correct way would be to use a Web Control:

    <asp:Button runat="server"
            OnClick="btnSubmit_Click" OnClientClick="return confirm('Sure?')" Text="Submit" />
    
    0 讨论(0)
提交回复
热议问题