views:

58

answers:

2

Hi

Is it possible to prevent the _doPostBack() call getting rendered on a button?

I would like add some custom logic prior to calling the postback.

I have added an onClick event to the button

e.g.

<button id="manualSubmit" runat="server" class="manual-submit" onclick="$('#jeweller-form').hide();"  />

However, this just gets rendered inline before the _doPostBack() But the postback gets fired before the jQueryHide takes place

I would like to call my own JS function then manually trigger the postback

any ideas?

A: 

Try this:

<button runat="server" id="Test" onserverclick="Test_ServerClick">Submit</button>

<script src="jquery-1.3.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function() {
        var o = $("#Test"), c = o.attr("onclick");
        o
        .removeAttr("onclick")
        .click(function(e) {
            o.fadeOut("slow", function() {
                o.fadeIn("slow", function() {
                    c(e);
                });
            });
            return false;
        });
    });
</script>
Mehdi Golchin
hi - I'm not using an <ASP:Button>. I am using <button runat="server>. Which does create a _doPostBack call. And doesn't have an OnClientClick event
Christo Fur
Sorry, I checked your code, but it didn't use `__doPostBack` as well.
Mehdi Golchin
Which version of ASP.NET you are using? BTW, replace the `button` with `<input type="button" />` element.
Mehdi Golchin
probably because I have an event handler wired up to the button (not via AutoEventWireUp, but manually in the codebehind). I imagine that is what creates the _doPostBack for the button
Christo Fur
Sorry for delay, I have edited my post. Take a look please.
Mehdi Golchin
A: 

Hey,

Add return false; after the client-side code in the click event. In the HTMLControl, it didn't think it rendered __doPostBack; is the control that renders the _doPostBack, and the common way to prevent that for that control is:

<asp:Button ... OnClientClick="doThis();return false;" />

Which renders these JS statements before __doPostBack.

HTH.

Brian
hi - I'm not using an <ASP:Button>. I am using <button runat="server>.
Christo Fur
OK, comment still applicable; as long as you return false before the end of the onclick event, the __doPostBack will not fire. Surprised there would be a postback... but there is also some intrinsic features to buttons too that you may be dealing with. Maybe the issue may also be related to the need to call preventDefault (info on that event: https://developer.mozilla.org/en/DOM/event.preventDefault) or stoppropogation (https://developer.mozilla.org/en/DOM/event.stopPropagation).
Brian