How to use [WebMethod] in server script blocks on aspx page?

I am trying to create a simple .Net 3.5 page that has HTML and WebMethod. When I try to call mine WebMethodfrom my browser, but it continues to save the page. Any ideas how to make it work?

This is my code:

<%@ Page Language="C#" Inherits="CFW.WebUI.Page" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

    [System.Web.Services.WebMethod]
    public static string Test()
    {
        return "Hola!";
    }

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

Note. I do not use Ajax.Net. I just want to call WebMethod from the client (using jQuery).

+3
source share
4 answers

The way to do this is as follows:

[WebMethod]
public static string SayHello()
{
     return "Hola";
}

On your page, you need to add ScriptManagerand set the EnablePageMethodsvalue of the property trueas follows:

<asp:ScriptManager id="sMgr" runat="server" EnablePageMethods="True" />

, , , - Javascript, SayHello, Javascript:

function InvokeSayHello()
{
     alert(PageMethods.SayHello()); //Will alert 'Hola'
}
0

    [System.Web.Services.WebMethod]

    [WebMethod]
    [System.Web.Script.Services.ScriptMethod]
0

, ScriptManager, PageMethod PageMethod. ASP , -, . yourpage.aspx/MethodName. [WebMethod], [ScriptMethod] - . <<26 > , GET, , .

0

You can also add [System.Web.Script.Services.ScriptMethod ()] to your method:

**

http://geekswithblogs.net/frankw/archive/2008/03/13/asp.net-ajax-callbacks-to-web-methods-in-aspx-pages.aspx

-1
source

All Articles