views:

59

answers:

1

I have a button that closes a modal dialog box on an ASP.NET Ajax form. How do a add a client side function to be called on the same button click?

currently firebug has this for the onclick

javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$DefaultContent$btnPaymentActionDetailClose", "", true, "", "", false, false))

can I just add a click handler with jQuery? I don't want to wipe out the postback as it is necessary for the forms functionality.

Can I use

$("#buttonId").bind("click", function(e){
   runAnotherFunction();
});

And not interfere with the existing postback?

Actually truth be told, I want to exec the function when the modal dialog closes. Do ajax control toolkit widgets fire any type of events? I know I can find the behavior and call show and hide. Can I attach my function to exec when modal.hide() is executed?

Thanks for any advice, ~ck in San Diego

+2  A: 

Yes, you can definitely have your ASP:Button run Javascript on the client before posting back. It's the OnClientClick property of the webcontrol.

You won't need jQuery for this.

<asp:button id="btn" runat="server" onClientClick="SomeJsFunction()" 
     onClick="ServerSideMethod()" Text="Hello Button" />

<script language='javascript'>
     function SomeJsFunction()
      {
         alert('hi');
      }
</script>

If you're interested controlling whether the postback occurs or not, you could return and check for true/false from the result of your Javascript function.

function SomeJsFunction()
{
     var isGood = false;
     return isGood;
}

<asp:button id="btn" runat="server" onClientClick="return SomeJsFunction()" 
     onClick="ServerSideMethod()" Text="Hello Button" />

(thanks to Ralph for the nudge in the comments)

p.campbell
+1 I'd add that you might want to have it return true or false, as in 'onClientClick="return SomeJsFunction()"' if you need to validate some input and/or conditionally cancel the postback. The JS function then obviously needs to return a boolean.
Rafe Lavelle