8

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 aspx file.

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

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

What am I doing wrong? Thanks

Mark
  • 1,368
  • 2
  • 13
  • 23
Deniz Dogan
  • 23,833
  • 33
  • 104
  • 154

8 Answers8

15

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" />
Yuriy Faktorovich
  • 62,163
  • 14
  • 99
  • 133
  • Yes, I saw this in Firebug, but I figured it basically meant I was "Doing It Wrong". I tried experimenting with `onclick="if (confirm('Sure?')) return;"`, but with that, the `__doPostBack` call wasn't there anymore! :( – Deniz Dogan Aug 07 '09 at 14:45
  • 1
    The reason for not using `` is that it uses the `INPUT`, while I need the `BUTTON` tag for easy CSS styling. The reason for needing `BUTTON` is not reflected in the question, just trust me on it. :) – Deniz Dogan Aug 07 '09 at 14:54
  • In that case you may be interested in: http://stackoverflow.com/questions/187482/how-can-i-use-the-button-tag-with-asp-net – Yuriy Faktorovich Aug 07 '09 at 15:02
  • your code is 100% right, but i wanted to mention that if you set the asp.net button UseSubmitBehavior=false, this code will not work and you will need to find anther way to handle it. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.usesubmitbehavior.aspx – Amr Elgarhy Jun 12 '10 at 23:45
3

I had more success with

<asp:Button ID="btnSubmit" runat="server" Text="Save" UseSubmitBehaviour="false"
OnClick="btnSubmit_Click" OnClientClick="if (!confirm('Sure?')) return" />
keith
  • 2,995
  • 2
  • 26
  • 34
2

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.

Mygod
  • 1,908
  • 1
  • 17
  • 37
1

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.

Tim S. Van Haren
  • 8,617
  • 2
  • 28
  • 34
Kelly Gendron
  • 6,819
  • 6
  • 40
  • 64
1

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>
Canavar
  • 46,286
  • 17
  • 83
  • 120
  • Tried that, but then it would just submit the `form` which wraps my HTML content, which is not what I'm trying to achieve. – Deniz Dogan Aug 07 '09 at 14:48
  • 1
    Your form is going to submit anyway - that's how the server-side onclick gets fired – n8wrl Aug 07 '09 at 15:02
1

Try this:

<button type="button" runat="server" id="btnSubmit"
OnServerClick="btnSubmit_Click" onclick="if(!confirm('Sure?')) return;">
Submit
</button>
MBahamondes
  • 350
  • 1
  • 2
  • 7
0

< 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.

n8wrl
  • 18,771
  • 4
  • 58
  • 100
0

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)
{
}
Willie Cheng
  • 5,589
  • 7
  • 39
  • 58