页面方法不在asp.net中响应

时间:2011-04-19 19:39:19

标签: asp.net asmx pagemethods

我在页面方法的帮助下调用两个服务器端功能但没有任何反应。 我在这里给我的代码。所以请告诉我我犯了什么错误,

我的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"] ;

        }
    }
}
请帮助我抓住错误。感谢

1 个答案:

答案 0 :(得分: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;"/>