2011-04-19 74 views
0

我打電話兩個服務器端功能的幫助頁面的方法,但沒有發生。 這裏我給我的代碼。所以請告訴我什麼錯誤,我提出,頁方法沒有響應在asp.net

我的aspx代碼

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %> 

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

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 

</head> 
<body> 
    <form id="form1" runat="server"> 
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"> 
    </asp:ScriptManager> 
    <div> 
    <a href="#" onclick="javascript:preview();">Test</a> 
     <br /> 
    </div> 
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 

    <br /> 
    <br /> 
    <asp:Button ID="btnSetvwState" runat="server" Text="Set Session Val" OnClientClick="SetSessionVal;return false;" /> 
     <asp:Button ID="btnGetVwState" runat="server" Text="Get Session Val" OnClientClick="GetSessionVal;return false;"/> 
    <script language="javascript"> 
     function preview() { 
      alert($get('TextBox1').value); 
      PageMethods.GetMessage("Hi", "Joy", onSuccess) 
      return false; 
     } 

     function onSuccess(res) { 
      alert(res); 
     } 

     function GetSessionVal() { 
      PageMethods.GetViewState(onSuccess) 
      return false; 

     } 

     function SetSessionVal() { 
      alert("pp"); 
      PageMethods.SetViewState("Hi", "Tridip", onSuccess) 
      return false; 

     } 
    </script> 
    </form> 
</body> 
</html> 

我的服務器端代碼


using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

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

     } 

     [System.Web.Services.WebMethod] 
     public static string GetMessage(string pr1,string pr2) 
     { 
      return pr1 + " " + pr2; 
     } 

     [System.Web.Services.WebMethod(EnableSession=true)] 
     public static void SetViewState() 
     { 
      HttpContext.Current.Session["data"] = "Hellp"; 

     } 

     [System.Web.Services.WebMethod(EnableSession = true)] 
     public static string GetViewState() 
     { 
      return (String) HttpContext.Current.Session["data"] ; 

     } 
    } 
} 

請幫我捉錯。感謝

+3

什麼是你看到?什麼不行,你看到了什麼? – 2011-04-19 19:42:02

+0

您是否嘗試過調試?你有沒有設置斷點?發生了什麼?我們不會爲您調試您的代碼,但如果您會說出您嘗試過的內容,我們將爲您提供幫助。 – 2011-04-19 19:47:16

+0

嘗試'OnClientClick =「SetSessionVal(); return false;」'並記住,返回false可能會導致服務器端單擊事件處理程序不能觸發。 – 2011-04-19 19:51:00

回答

2

失蹤()這裏(SetSessionValGetSessionVal):

<asp:Button ID="btnSetvwState" 
      runat="server" Text="Set Session Val" 
      OnClientClick="SetSessionVal();return false;" /> 
    <asp:Button ID="btnGetVwState" 
       runat="server" Text="Get Session Val" 
       OnClientClick="GetSessionVal();return false;"/> 
+0

不能肯定地說這是他的問題,但它對我來說也很奇怪。 – 2011-04-19 19:51:49

+0

是有遺漏()但當我使用OnClientClick =「SetSessionVal();返回false;」那麼pagemethod也不會被調用。我無法捕捉到錯誤。所以請告訴我缺少什麼。 – Thomas 2011-04-20 08:21:19