shanish
shanish

Reputation: 1984

Calling jQuery function after the execution of server side onClick event of button.

In a aspx page, If we use onClientClick and onClick for a same control, the onClientClick event fires first and then onClick will fire.

I want to do something after firing the onClick event. For example, for a Button control I've written code for its click event(button1_click). After the execution of this code, I need to fire some jQuery function, is it possible. can anyone help me?

Upvotes: 0

Views: 4115

Answers (3)

Tim B James
Tim B James

Reputation: 20374

If you are wanting to call a jQuery function after a page has post back, then there are a few ways you can do this.

One of which is using the Page.RegisterStartupScript or Page.RegisterClientScriptBlock. Both of these will output javascript onto your page after the postback.

Another way, which I tend to do, is create a public string on the page public string _JS, then on my .aspx page I have the following code;

<script>
    $(function(){ <%=_JS%> });
</script>

This way you can build up the _JS string and you know that it will fire after your DOM has loaded.

Upvotes: 1

profanis
profanis

Reputation: 2751

You can have just the onClientClick="PostData"

<script language="javascript">
  function PostData(){
     //do something here
     __doPostBack("triggerBTN", "@@@@" + _paymentPageData);
  }
</script>

In you aspx page add a hidden button

 <asp:Button runat="server" ID="triggerBTN" Visible="false" />

In the codebehind page

var postBackArg = Request["__EVENTARGUMENT"];
        if (postBackArg != null)
            if (postBackArg.StartsWith("@@@@")) {
               //do your business logic here
            }

I hope it helps you

Upvotes: 1

angus
angus

Reputation: 690

At the end of the button click event routine, use Page.RegisterStartupScript to call the JQuery function using javascript. Example in link.

http://msdn.microsoft.com/en-us/library/system.web.ui.page.registerstartupscript.aspx

Upvotes: 2

Related Questions