pita
pita

Reputation: 537

jquery button trigger server side onclick event

I am using a jquery dialog, when user click ok, the server side onclick event should be fired, if click cancel, nothing happened.

i have to prevent the click function at the beginning by preventDefault() function.

  $(document).ready(function () {
         $("#<%=submit.ClientID %>").click(function (event) {
             event.preventDefault();
                $("#confirm").dialog({
                    buttons: {
                        "OK": function () {  $(this).dialog("close"); 
                              Here should be the code to trigger the server side click event


                     },
                        "Cancel": function () { $(this).dialog("close");},
                    } 
                });
         });
     });

I don't know how to trigger a server side onclick event. any ideas? thanks

Upvotes: 2

Views: 17573

Answers (4)

pita
pita

Reputation: 537

https://stackoverflow.com/a/10583339/1202242

I used two button, and one is hidden. It solved my problem perfectly

Upvotes: 1

Adriano Repetti
Adriano Repetti

Reputation: 67148

It looks a little bit dependant on implementation details (even if they're so widely used that they won't be changed) but code is this:

__doPostBack('submit','OnClick');

This will execute the OnClick event handler for the control named submit. As reference take a look to this little tutorial about how postbacks works in ASP.NET.

Upvotes: 0

Sagiv Ofek
Sagiv Ofek

Reputation: 25280

i think you are confusing between server and client sides. if you want to trigger event on the server side you need to notify him (can be done by ajax call). the click event is a client side event that will not do anything on the server side. try to put some code under the "OK" function to notify the server whatever you want like via ajax call.

anyway you should move the event.preventDefault() call into the "Cancel" function.

edit: another way to approach it is to prevent the submit to happen if you don't want it. at your form tag add onsubmit="foo()" and define:

function foo(){
   //call your dialog and return true to continue the submit and false to cancel.
}

Upvotes: 0

lucuma
lucuma

Reputation: 18339

Put the event.preventDefault() in the cancel part or some kind of condition for it so it isn't running on every click.

Upvotes: 0

Related Questions