0

my anchor code is like below :

<a id="PaymentLink" name="PaymentLinks" runat="server"
        onserverclick="PaymentLink_Click" title="Payment"></a>

form element like below :

<form id="form1" runat="server" onsubmit="alert('alibaba');return false;">

and submit function of jquery is like below :

            $(function () {
                $('#form1').submit(function (e) {
                    alert('alert2');
      });
});

why after click that anchor we only see alibaba alert ...
link button has this manner too...
but when we use regular asp.net buttons , we will see both alerts ...
how can we trigger submit function of jquery when that anchor is clicked ?

i need to use server side anchor for some reasons ...

thanks in advance

SilverLight
  • 19,668
  • 65
  • 192
  • 300

3 Answers3

1

How to capture submit event using jQuery in an ASP.NET application?

Community
  • 1
  • 1
Yuriy Rozhovetskiy
  • 22,270
  • 4
  • 37
  • 68
0

I took the solution mentioned by Yuriy and put it in a jQuery plugin: http://plugins.jquery.com/beforePostBack/.

Peter
  • 13,733
  • 11
  • 75
  • 122
0

Because the anchor (HyperLinkButton) is set to run on server and it fires onServerClick, the page is posted back to server and your client-side $('#form1').submit() function is not fired. You have to decide, how do you want to handle onClick event: server-side or client-side. You could also consider having the linkbutton call a WebMethod asynchronously and then handle callback on client.

Dimitri
  • 6,923
  • 4
  • 35
  • 49
  • really thanks for answer / but need both server and client codes / besides i disabled postback by return=false; in form element. so there is no postback here (... and your client-side $('#form1').submit() function is not fired). if you are right , what about regular button.in regular button we can have both server and client side codes and it is a server side control ... every thing is ok about regular button in asp.net , but this anchor -> i think this anchor should act like regular asp.net buttons. any idea ? – SilverLight Jun 27 '11 at 17:55
  • What happens is the client side $('#form1').submit() fires before postback, and alert shows up before postback, but since the page is refreshed after postback you don't see the alert popup. Plus you added return false to form's submit event. You have to add it to button's onclick event to prevent postback, but if you prevent postback you don't get server-side processing. Check out webmethods. It's a nice way of having server calls from client side. – Dimitri Jun 27 '11 at 18:09