简体   繁体   中英

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

event.preventDefault()放在取消部分或某种条件下,以便它不会在每次单击时都运行。

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

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 .

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM