2

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

Adriano Repetti
67.4k20 gold badges143 silver badges214 bronze badges
asked May 11, 2012 at 18:50
3
  • 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. Commented 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" Commented May 11, 2012 at 19:01
  • Did one of the answers help you? If so, please mark the answer or provide feedback. Commented May 17, 2012 at 22:42

4 Answers 4

1

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

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

answered May 18, 2012 at 12:53

Comments

0

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

answered May 11, 2012 at 18:56

Comments

0

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.
}
answered May 11, 2012 at 18:57

4 Comments

if I move the event.preventDefault() into the "cancel" function, the server side code will run when click the "submit" button before user click the "ok" on the popuped dialog
so if you don't want to create a submit you need to put it at the beginning (like you did) and call $("#your_form_id").submit() manually on "OK" function or create an ajax call instead.
that is my problem, I tried to use $("#your_form_id").submit() which didn't fire submit_onClick() function on the server side, and i don't know how to use an ajax, could you give me some example?
i updated my comment. your dialog function should return a value. pass this value to the function called on submit event.
0

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.

answered May 11, 2012 at 18:58

1 Comment

@Pita I remember some issues (with some some browsers) related to buttons with ID "submit". I'm not sure so any information will be welcome. Anyway this should be work (I use it too) but you may have to rename your button to something else.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.