Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi,

I need to get a solution for how to prevent a page post back on a button click event occurs. But I need to still continue with the Button_Click event and only need to prevent the post back.

I already tried with OnClientClick="return false;" this completely preventing to call the Button_Click event.

Your help will he highly appreciated.
Posted

You can't. And you need to understand how the web works to understand why you can't. C# runs on the server and for it to run the client has to postback.

Now, what you can do is change the button click to call JavaScript that then calls your code as a webservice. Look at jQuery's $.ajax() method.
 
Share this answer
 
Comments
[no name] 30-Oct-14 9:36am    
Thank you Ryan for your quick reply

By the way, Can u explain "change the button click to call JavaScript that then calls your code as a webservice" what you realy ment?
Thanks7872 30-Oct-14 9:40am    
Atleast try something your self. He meant this:

http://www.aspsnippets.com/Articles/Calling-ASPNet-WebMethod-using-jQuery-AJAX.aspx
ZurdoDev 30-Oct-14 9:42am    
That looks like a good link. Thanks for contributing.
ZurdoDev 30-Oct-14 9:41am    
OnClientClick="callWebService(); return false;"

and then create a JS file that has a function named callWebService() which is where you'll call your webservice. Just google for examples of how to use .ajax() if you haven't used it before. There are tons of examples online.
Use Update panel for avoiding full page loading...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900