【问题标题】:Page Method not responsing in asp.net页面方法在 asp.net 中没有响应
【发布时间】:2012-09-24 17:25:28
【问题描述】:

我在页面方法的帮助下调用了两个服务器端函数,但什么也没发生。 我在这里给出我的代码。所以请告诉我我犯了什么错误,

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

        }
    }
}

请帮我找出错误。谢谢

【问题讨论】:

  • 你在看什么?什么不起作用,你看到了什么?
  • 你试过调试这个吗?你设置断点了吗?发生了什么?我们不会为您调试代码,但如果您说出您的尝试,我们会为您提供帮助。
  • 试试 OnClientClick="SetSessionVal();return false;" 并记住,返回 false 会导致服务器端点击事件处理程序不触发。

标签: asp.net asmx pagemethods


【解决方案1】:

这里缺少()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;"/>

【讨论】:

  • 不能肯定这会导致他的问题,但我也觉得这很奇怪。
  • 是的,缺少 () 但是当我使用 OnClientClick="SetSessionVal();return false;"然后也没有调用pagemethod。我无法捕捉到错误。所以请告诉我缺少什么。
猜你喜欢
  • 1970-01-01
  • 2023-01-11
  • 1970-01-01
  • 2022-12-05
  • 2011-08-19
  • 1970-01-01
  • 1970-01-01
  • 2022-11-12
  • 1970-01-01
相关资源
最近更新 更多