4

I have an asp button that is used to upload images (using uploadify) and it has also code behind . The problem is I want to run the client side script before postback. but the postback occurs while uploading images .

<ASP:BUTTON ID="Button1" runat="server" onclick="Button1_Click" onclientclick="Save();"/> 

How can I force it to finish the client script first ?

4
  • 1
    I thought that the Postback would happen after the onCLientClick has finished. Ok, then you could use a input type="button" and trigger the Postback manually(__doPostBack('Button1,'');). But i'm really sure that the postback happens after the onclientclick has finished, because you can prevent a PostBack while you are returning false from the onClientClick. Commented Jun 12, 2011 at 22:29
  • @Tim I think so to. The postback will not occur until the OnClientClick handler returns (or returns true) and if OnClientClick returns false then the postback will be cancelled. Commented Jun 12, 2011 at 22:34
  • @Bala: I think HTB should debug the javascript because it's an odd behaviour. I've posted my comment as answer even though it's not a direct answer to his question. Commented Jun 12, 2011 at 22:46
  • I thought that too . But If the script is taking long time (uploading a number of images) it will postback before the script is finished . Commented Jun 12, 2011 at 23:13

2 Answers 2

4

I thought that the Postback would happen after the onClientClick has finished. OK, then you could use a input type="button" and trigger the Postback manually(__doPostBack('Button1,'');).

But I'm pretty sure that the postback happens only after the onClientClick has finished, because you can prevent a PostBack by returning false from the onClientClick. You should debug your javascript, because this behaviour is exceptional.

Sign up to request clarification or add additional context in comments.

6 Comments

I did this <input id="Button1" type="button" value="button" onclick="Save(); " /> in Save(){ 1) my script . 2) (__doPostBack('Button1,'');) . but it still postbacks before script finishes ??
actually, it is posting back without running button code behind (I put break point)
@HTB: as i've mentioned you should debug the script to see what happens. I'm not familiar with uploadify, but the files might be uploaded asynchronously, hence the function returns before it's really finished. But as i've seen uploadify has an event that you could use to trigger the postback: onAllComplete
This is exactly what was happening . The function returns before it's really finished . I used onAllComplete and it worked . Thank you so much Tim . One thing left if you do not mind . How can I link the button server side function to it ?
@HTB: The eventargument provided as first parameter of __doPostBack causes ASP.NET to trigger the appropriate serverside event. See my example in my answer.
|
2

I came across similar case; for anyone who would face a simlar issue; my resolution was as

<ASP:BUTTON ID="Button1" runat="server" onclick="Button1_Click" onclientclick="return Save();"/> 

Note that the onclientclick has the "return" keyword; this way when onclientclick is true onclick would fire; otherwise it would not.

1 Comment

Thanks! This worked for me also. I used it to create a "spinner" in CSS. The javascript function showed the "spinner" <div> by setting display: block.Then the PostBack occurred and the page refreshed showing the new content. I was able to insert the Javascript and CSS inside the <asp:contentplaceholder> directly.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.