Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,
I am trying to call a code behind function using jquery but page is refershing when function is called, any help will be appreciated


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js" type="text/javascript"></script>
		



<script type = "text/javascript">
    function ShowCurrentTime() {
        $.ajax({
            type: "POST",
            url: "default2.aspx/ServerSideMethod",
            data: '{}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: true,
            cache: false,
            success: OnSuccess,
            failure: function (response) {
                alert(response.d);
            }
        });
    }
    function OnSuccess(response) {
        alert(response.d);
    }
</script>


--------------------------------

.cs function



[System.Web.Services.WebMethod]
   public static string ServerSideMethod()
   {
       return "Hello, this is string message";
   }
Posted
Updated 26-Jul-11 3:32am
v3
Comments
Ankur\m/ 26-Jul-11 9:40am    
How you are calling it? It works fine for me.
suniltikli 26-Jul-11 10:36am    
I am using the below code to call the function

<asp:Button ID="Button2" runat="server" OnClientClick="ShowCurrentTime();" Text="New Button" />

Onclientclick must return false to avoid a postback.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 26-Jul-11 14:12pm    
Yes indeed! 5+
XML
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js" type="text/javascript"></script>
<script type = "text/javascript">
    function ShowCurrentTime() {
        $.ajax({
            type: "POST",
            url: "default2.aspx/ServerSideMethod",
            data: '{}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: true,
            cache: false,
            success: OnSuccess,
            failure: function (response) {
                alert(response.d);
            }
        });
        return false; // Will stop the event bubbling and thus keep from submitting the form.
    }
    
    function OnSuccess(response) {
        alert(response.d);
    }
</script>


ASP
<!--  return ShowCurrentTime(); // The return is important here other wise the submit event would still occur -->
<asp:Button ID="Button2" runat="server" OnClientClick="return ShowCurrentTime();" Text="New Button" />


Best Regards,

—MRB
 
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