繁体   English   中英

PageMethods无法与VS2013 Web应用程序一起使用

[英]PageMethods Not Working with VS2013 web application

我整天都在努力处理此错误,并且阅读了几乎所有文章,并尝试了所有发现的提示,但仍然无法使它起作用。

请注意: ScriptManager中的EnablePageMethod = true,方法是公共的和静态的,并用[WebMethods]装饰

相同的编码对于VS2010和VS2013中的空项目都可以完美地工作,但是在与New Project-> web- > Web Forms一起使用时 ,输出附在此处供您参考。 错误输出

这是我的代码。

背后的代码:

[System.Web.Services.WebMethod]
        public static string GetCurrentTime(string name)
        {
            return "Hello " + name + Environment.NewLine + "The Current Time is: "
                + DateTime.Now.ToString();
        }

JavaScript:

<script type="text/javascript">
        function ShowCurrentTime() {
            PageMethods.GetCurrentTime(document.getElementById("<%=txtUserName.ClientID%>").value, OnSuccess);
           }
           function OnSuccess(response, userContext, methodName) {
               alert(response);
           }
    </script>

HTML:

<div>
        Your Name : 
            <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
        <input id="btnGetTime" type="button" value="Show Current Time" onclick="ShowCurrentTime()" />
    </div>

尝试将[System.Web.Services.WebMethod][System.Web.Script.Services.ScriptMethod]到服务器端GetCurrentTime方法。

还要确保您的脚本管理器具有EnablePageMethods="True" 您的问题指出您设置的EnablePageMethod = true拼写错误(应为复数形式)。

您需要使click事件处理程序失败,以防止回发/页面提交。

只需从您的javascript函数返回false:

function ShowCurrentTime() {
    PageMethods.GetCurrentTime(document.getElementById("<%=txtUserName.ClientID%>").value, OnSuccess);
    return false; // added
}

从客户端调用PageMethod时,必须确保该按钮不会回发。 使用您当前的标记,它将回传并导致意外的效果。

您应该对按钮使用以下标记,因此它永远不会回发。 然后对pagemethod的调用应该起作用。 在按钮下面的标记中,单击事件返回一个假值,因此该页面永远不会提交(即回发)。

另外,最好是完整地张贴页面标记,因为标记可能存在问题。

<input id="btnGetTime" type="button" value="Show Current Time"
                    onclick="ShowCurrentTime();return false;" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM