本文介绍了如何调用使用ScriptManager的特定类的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了有一些方法我想从JavaScript调用特定的服务器控件。
我知道的ScriptManager允许调用Web服务或页面方法。但我想打电话给一个特定的类方法(我可以国歌,甚至AjaxPro.NET做到这一点)。有没有办法做到这一点使用的ScriptManager?

I've created a particular server control that has some methods I'd like to call from javascript. I know that the ScriptManager allows to call a web service or a page method. But I'd like to call a particular class method (I can do this with Anthem or even AjaxPro.NET). Is there a way to accomplish this using ScriptManager?

感谢。

推荐答案

除了Web服务(ASMX或WCF)也可以调用页面方法。这些都是装饰用[WebService的]属性的页面的静态方法。您还需要脚本经理的EnablePageMethods属性设置为true。这里是一个快速示例:

Apart from web services (asmx or WCF) you can also call page methods. Those are static methods of your Page decorated with the [WebService] attribute. You also need to set the EnablePageMethods property of the script manager to true. Here is a quick sample:

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Services" %>

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


<script runat="server">

    [WebMethod]
    public static int AddOne(int arg)
    {
        return arg + 1;
    }

</script>
<html>
<head id="Head1" runat="server">
</head>

<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" 
            runat="server" EnablePageMethods="true">
        </asp:ScriptManager>

        <script type="text/javascript">
        function doAdd ()
        {
            PageMethods.AddOne(2);
        }
        </script>

        <a href="javascript: doAdd()">click me</a>
        </form>
</body>
</html>

这篇关于如何调用使用ScriptManager的特定类的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 13:34