OnClientClick is stopping postback

I have a set of ASP.NET controls:

<asp:LinkButton ID="Find"
    ClientIDMode="Static"
    runat="server"
    CausesValidation="true"
    OnClientClick="$('#Find').attr('disabled', 'disabled'); $('#SearchingLabel').show();">
    <span>Search</span>
</asp:LinkButton>
<asp:Label ID="SearchingLabel"
    ClientIDMode="Static"
    runat="server"
    Text="Searching..."
    style="display: none;" />

and as you can see I’m consuming the OnClientClick so that I can disable the Find button and show the SearchingLabel (and the JavaScript works without error BTW). Pretty basic stuff.

Further, surrounding the CausesValidation attribute, I do have validation controls on the page, but there are no current validation errors.

However, even though I’m not returning false from the JavaScript the page isn’t posting back. I’ve even attempted to return true; but that didn’t change anything (not really that surprising but it was worth a try).

I look forward to your feedback!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

It seems like you are disabling your button, before the postback.

You could try your page/script without the disabling part in it:

OnClientClick="$('#SearchingLabel').show();"

If this works, try it with a short delay:

OnClientClick="setTimeout(function() { $('#Find').attr('disabled', 'disabled'); }, 100); $('#SearchingLabel').show();"

Method 2

Try this js code onload

Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequest);
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequest);
function BeginRequest(sender, e) {
    e.get_postBackElement().disabled = true;
    // or your code

}
function EndRequest(sender, e) {
   .......
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x