从 UpdatePanel 设置会话变量

发布于 2024-08-29 07:06:47 字数 2847 浏览 1 评论 0原文

我正在使用 ASP.NET 2.0 AJAX Extensions 1.0 和 AJAX Control Toolkit 的 v1.0.20229 版本(据我所知,这是 .NET 2.0/Visual Studio 2005 的最新版本)。

我的网页 (aspx) 在 UpdatePanel 上有一个 DropDownList 控件。在 DropDownList 的 SelectedIndexChanged 事件的处理程序中,我尝试设置一个会话变量。

第一次触发该事件时,我收到 Sys.WebForms.PageRequestManagerParserErrorException:“无法解析从服务器收到的消息”。如果我继续,后续的 SelectedIndexChanged 将成功处理。

我偶然发现了一种解决方案,如果我在 Page_Load 中初始化会话变量(因此事件处理程序只是设置已存在的会话变量的值,而不是创建新变量),则问题就会消失。

我很高兴这样做,但我很好奇根本原因是什么。谁能解释一下吗?

(我怀疑设置会话变量会从服务器接收响应,然后将其返回给“调用者”,但这不是它知道如何处理导致异常的那种响应?)

编辑:我在中重现了该问题一个单独的小项目:

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SessionTest._Default" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">    
        <asp:ScriptManager ID="ScriptManager1" runat="server" />

        <div>

            <asp:UpdatePanel id="upCategorySelector" runat="server">
                <ContentTemplate>

                    Category:
                    <asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCategories_SelectedIndexChanged">
                        <asp:ListItem>Item 1</asp:ListItem>
                        <asp:ListItem>Item 2</asp:ListItem>
                        <asp:ListItem>Item 3</asp:ListItem>
                    </asp:DropDownList>

                </ContentTemplate>
            </asp:UpdatePanel>

        </div>

    </form>
</body>
</html>

Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace SessionTest
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // If I do this, the exception does not occur.
            if (Session["key"] == null)
                Session.Add("key", 0);
        }

        protected void ddlCategories_SelectedIndexChanged(object sender, EventArgs e)
        {
            // If Session["key"] has not been created, setting it from
            // the async call causes the excaption
            Session.Add("key", ((DropDownList)sender).SelectedValue);
        }
    }
}

I am using ASP.NET 2.0 AJAX Extensions 1.0 with the version v1.0.20229 of the AJAX Control Toolkit (which to my knowledge is the latest for .NET 2.0/Visual Studio 2005).

My web page (aspx) has a DropDownList control on an UpdatePanel. In the handler for the DropDownList's SelectedIndexChanged event I attempt to set a session variable.

The first time the event is fired, I get a Sys.WebForms.PageRequestManagerParserErrorException: "The message received from the server could not be parsed". If I continue, subsequent SelectedIndexChanged's are handled successfully.

I have stumbled upon a solution whereby if I initialise the session variable in my Page_Load (so the event handler is just setting the value of a session variable that already exists as opposed to creating a new one) the problem goes away.

I'm happy to do this, but I'm curious as to exactly what the underlying cause is. Can anyone explain?

(My suspicion is that setting the session variable receives a response from the server which is then returned to the 'caller', but it's not the sort of response it knows how to deal with causing the exception?)

EDIT: I reproduced the problem in a seperate little project:

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SessionTest._Default" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">    
        <asp:ScriptManager ID="ScriptManager1" runat="server" />

        <div>

            <asp:UpdatePanel id="upCategorySelector" runat="server">
                <ContentTemplate>

                    Category:
                    <asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCategories_SelectedIndexChanged">
                        <asp:ListItem>Item 1</asp:ListItem>
                        <asp:ListItem>Item 2</asp:ListItem>
                        <asp:ListItem>Item 3</asp:ListItem>
                    </asp:DropDownList>

                </ContentTemplate>
            </asp:UpdatePanel>

        </div>

    </form>
</body>
</html>

Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace SessionTest
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // If I do this, the exception does not occur.
            if (Session["key"] == null)
                Session.Add("key", 0);
        }

        protected void ddlCategories_SelectedIndexChanged(object sender, EventArgs e)
        {
            // If Session["key"] has not been created, setting it from
            // the async call causes the excaption
            Session.Add("key", ((DropDownList)sender).SelectedValue);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文