Click here to Skip to main content
15,886,038 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I annotated my code behind with

[WebMethod]
public void Test()
{
// some code
}

and tried to call this function using jquery in my aspx page....

but it is not working....

What is the proper way to achieve a task like this.... Thanks in advance
Posted

You can call [WebMethod] from javascript (without jquery) with PageMethods.Test().

To call webmethod from javascript you have to add
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
in your aspx page.
 
Share this answer
 
Comments
pavankumar1990 14-Mar-14 6:03am    
hey this my aspx page code

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="Test._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">


Welcome to ASP.NET!


<p>
<asp:Button ID="Button1" runat="server" Text="Call method" OnClientClick="callMyMethod()" />

</p>
<%--<p>
You can also find documentation on ASP.NET at MSDN.
</p>--%>
<script type="text/javascript">
function callMyMethod() {
PageMethods.method();
}
</script>
pavankumar1990 14-Mar-14 6:03am    
and this is my code behind code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
//using System.Web.Script.Services;

namespace Test
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}


[WebMethod]
public static void method()
{
HttpContext.Current.Response.Write("method from my code behind");
}
}
}
pavankumar1990 14-Mar-14 6:04am    
but still not working if you find anything wrong in the code plz let know
thanks in advance
define method :
public static void Test(){

}
 
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