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
-
The only way to do anything on the server-side from javascript is to either send an ajax request, or submit a form either through the current page or an iframe. How can you have a click event on the server-side? the GUI doesn't exist on the server-side.Kevin B– Kevin B2012年05月11日 18:55:48 +00:00Commented May 11, 2012 at 18:55
-
@KevinB I am newbie of jquery. as I know, the asp.net button has onclick event and onclientclick event, onclick event is on the server side, i want to fire that event when user click "ok"pita– pita2012年05月11日 19:01:58 +00:00Commented May 11, 2012 at 19:01
-
Did one of the answers help you? If so, please mark the answer or provide feedback.lucuma– lucuma2012年05月17日 22:42:28 +00:00Commented May 17, 2012 at 22:42
4 Answers 4
https://stackoverflow.com/a/10583339/1202242
I used two button, and one is hidden. It solved my problem perfectly
Comments
Put the event.preventDefault()
in the cancel part or some kind of condition for it so it isn't running on every click.
Comments
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.
}
4 Comments
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.