页面方法不起作用

时间:2014-02-21 16:03:08

标签: c# javascript asp.net

我正在尝试在我的asp .net项目中使用页面方法,但我不知道它为什么不起作用。

基本上我有2个页面:locations.aspx和details.aspx。

在locations.aspx中,当我点击图像时,运行一个调用页面方法的JS函数。然后在该页面方法中,我存储了一个我在Details.aspx页面中使用的会话值。由于某种原因,页面方法失败,我收到警报(我已在onFailed()方法中设置了警报)。请告诉我是否有任何错误或我是否应采取任何程序。

我已经测试过:

  1. onclick事件正在发生。
  2. 如果我手动为会话变量设置一个值,那么它在Details.aspx页面中工作。
  3. Location.aspx

        <form id="form1" runat="server">
    
                <img class="..." src="...." usemap="#panchagarh-map" />
                    <map name="panchagarh-map">
                        <area onclick="javascript:passLocationString('Panchagarh')" href="Details.aspx" class="...." coords=" .... " shape="poly"
                    </map>
    
    
    
            <script language="javascript" type="text/javascript">
                function passLocationString(locationString) {
                    PageMethods.setLocationString(locationString, onSucceeded, onFailed);
                }
                function onSucceeded(result, userContext, methodName) {
                }
                function onFailed(error, userContext, methodName) {
                    alert("An error occurred")
                }
            </script>
    
            <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
            </asp:ScriptManager>
        </form>
    

    Location.aspx.cs

    public partial class Locations : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
            }
    
            [System.Web.Services.WebMethod(EnableSession = true)]
            public static void setLocationString(string temp)
            {
                HttpContext.Current.Session["locationString"] = temp;
            }
        }
    

    Details.aspx.cs

    protected void Page_Load(object sender, EventArgs e)
    {
        HtmlGenericControl parentDiv = new HtmlGenericControl("DIV");
        parentDiv.ID = "test";
        parentDiv.InnerHtml = (string)Session["locationString"];
    
        parentDiv.Visible = true;
        Panel.Controls.Add(parentDiv);
    }
    

1 个答案:

答案 0 :(得分:0)

虽然看到错误的文本会很高兴。我建议尝试使用Web服务而不是页面方法,看看它是否有效。另一个想法是检查会话是否在Web.config中正确配置:

configuration>
 <system.web>
   <sessionState mode="InProc"/>
 </system.web>
</configuration>