Deniz Dogan Deniz Dogan - 1 month ago 15
ASP.NET (C#) Question

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

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 immediately in my


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

This works just fine as long as I don't have the
attribute there, i.e. the
handler is fired as it should. But when I use the
attribute it is not, no matter whether I confirm or decline the confirmation dialog box.

What am I doing wrong? Thanks


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" />