4

I have an asp.net app with an asp:button that causes a postback, this means that the html for the input has an onclick attribute that is auto generated, I need to add an event via jQuery to the onclick attribute that fires before the auto generated postback is fired.

Below is an example of the html generated, i need to make sure that my event added by jQuery fires before the WebForm_DoPostBackWithOptions() function.

<input id="ctl00_MainContent_Save" type="submit" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$MainContent$Save", "", true, "", "", false, false))" value="" name="ctl00$MainContent$Save">

for more clarity, I know I can add an event via jQuery like this:

$('#ctl00_MainContent_Save').click(function() {
    somfunction();
});

but i want my event to fire before the WebForm_DoPostBackWithOptions

3
  • What do you want to do before the page postback? Commented Jul 28, 2011 at 15:09
  • call any javascript function, an alert will do, i have updated my post hope it helps. cheers. Commented Jul 28, 2011 at 15:50
  • Thank you so much for having the exact same problem. And thanks ShankarSangoli for your reply. Commented Oct 14, 2011 at 12:31

3 Answers 3

4

You can probably try mousedown event which will be fired before the click event is triggered.

$('#ctl00_MainContent_Save').mousedown(function() {
    somfunction();
});

or try the below code

$(document).ready(function(){

  $('#ctl00_MainContent_Save')
  .attr("onclick", null)
  .removeAttr("onclick")
  .click(function() {
        somfunction();
        WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions($(this).attr("name"), "", true, "", "", false, false));
    });

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

1 Comment

Great stuff, I used the second option, I was hoping that jQuery could sort the events somehow but this solved my problem cheers.
2

Hope this will give you an idea on how to do.

<asp:Button ID="bsrc" runat="server" Text="Search" OnClick="bsrc_Click" />


<script type="text/javascript" language="javascript">
     $(document).ready(function()
     {
         $('#<%= bsrc.ClientID %>').click(function(e) 
         {              
                return true;
         });
     });
</script>

When you will click on button first it will call the javascript function and then will go for server side. If you want to stop calling server side event from javascript code just return false; in place of return true;

1 Comment

The event has to be added via jQuery I do not want more javascript in the html.
0

How about:

<input id="ctl00_MainContent_Save" type="submit" value="" 
   name="ctl00$MainContent$Save">
...


$('#ctl00_MainContent_Save').click(function(e) {
   e.preventDefault();
   somfunction();
   WebForm_DoPostBackWithOptions(
       new WebForm_PostBackOptions("ctl00$MainContent$Save",
        "", true, "", "", false, false))"
 });

If you can't alter the HTML, you could try (warning: untested!)

var $link = $('#ctl00_MainContent_Save');
var oldFunction = $link[0].onClick;
$link[0].onClick = null
$link.click(function(e) {
   e.preventDefault();
   somfunction();
   oldFunction();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.