2016-11-12 27 views
0

不工作我花了一整天,出現此錯誤掙扎,看了幾乎所有的文章,並想盡提示我發現,但仍然不能使它發揮作用。PageMethods與VS2013 Web應用程序

請注意:裏面的ScriptManager EnablePageMethod = TRUE,方法是公共的和靜態的,並飾以[的WebMethod]

相同編碼時使用新的工作都完善與VS2010和與VS2013空項目,但項目 - > web - > web表格輸出附在這裏供您參考。 Error OutPut

這是我的代碼。

代碼背後:

[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> 
+0

您可以粘貼您正在使用的頁面的代碼前端嗎?我的意思是你的網頁的完整標記。 – Sunil

回答

0

嘗試增加兩者[System.Web.Services.WebMethod][System.Web.Script.Services.ScriptMethod]到服務器端GetCurrentTime方法。

另外,還要確保你的腳本經理EnablePageMethods="True"。你的問題規定設置EnablePageMethod =真這是拼寫錯誤(應該是複數)。

+0

嘗試過他們但仍然不工作..! – BilalAhmed

+0

嘗試打開瀏覽器開發人員工具,查看記錄到控制檯的錯誤。將記錄的錯誤消息添加到您的問題將非常有幫助。 –

0

您需要失敗的單擊事件處理程序,以防止回發/網頁提交。

只需使用javascript函數返回false:

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

你必須確保從調用客戶端的一個PageMethod的按鈕時,沒有後回來。使用您當前的標記,它會回發並導致意想不到的效果。

你應該使用按鈕下面的標記,所以它永遠不會回發。然後調用pagemethod應該可以工作。在下面的標記中,按鈕點擊事件返回一個錯誤值,所以頁面永遠不會被提交,即返回。

此外,如果您發佈的網頁標記滿,因爲有可能是標記的一個問題那將是最好的。

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