May 15, 2009

Using jQuery to Prevent ASP.NET Server Side Events from Occuring




I recently was wiring up the ASP.NET Button control with jQuery and checking out the different events related to the same. I also wanted to see if it was possible to stop a server side event from occuring using jQuery.

For example: I have an ASP.NET button control which causes a postback when it is clicked. However what if you want to check a condition at ClientSide and then allow the postback to occur. Here's how to prevent a postback from occuring using jQuery.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


 


<html xmlns="http://www.w3.org/1999/xhtml">


<head runat="server">


    <title></title>


    <script src="Scripts/jquery-1.3.2.js" type="text/javascript"></script>


    <script type="text/javascript">


        $(document).ready(function() {


            $("#<%=Button1.ClientID %>").click(function() {


                // check a condition


                if ("a" == "b")


                    return true;


                else


                    return false;


            });


        });


    </script>


</head>


<body>


    <form id="form1" runat="server">


    <div>


        <asp:Button ID="Button1" runat="server" Text="Click Me"


            onclick="Button1_Click" />


    </div>


    </form>


</body>


</html>




'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

0 Responses to "Using jQuery to Prevent ASP.NET Server Side Events from Occuring"
 

Copyright © 2009-2012 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions