从iframe中的页面检索会话的PageMethods不起作用

本文关键字:会话 PageMethods 不起作用 面检索 iframe | 更新日期: 2023-09-27 18:03:02

2页。上传。

Uploadee。在Uploader.aspx的iframe中。

这是Uploader.aspx的标记:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Uploader.aspx.cs" Inherits="Uploader" EnableEventValidation="false" %>
    <!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>
<script type="text/javascript">
    function doupload()
    {
        document.getElementById('spFN').innerHTML = 'Document upload in progress.';
        window.frames[0].document.forms[0].submit();
        intervalID = window.setInterval(function ()
        {
            PageMethods.GetUploadStatus(function (result)
            {
                if (result)
                {
                    alert(result.fileNumber);
                    document.getElementById('spFN').innerHTML = 'Uploading file ' + result.fileNumber;
                }
            });
        }, 500);
    }  
</script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
    <div>
    <span onclick="doupload();">Try it</span> <span id="spFN"></span>
    <iframe id="uploadFrame" frameborder="0" height="400" width="800" scrolling="yes" src="Uploadee.aspx"></iframe>
    </div>
    </form>
</body>
</html>

我正在尝试从iframe中的页面读取会话变量的值。以下是iframe (uploadee.aspx)页面后面的页面代码。

    public partial class Uploadee : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            for (int i = 0; i < 3; i++)
            {
                Session["FN"] = i.ToString();
                Thread.Sleep(1000);
            }
        }
    }
}

所以,我在一个循环中增加会话变量- session ["FN"] -每次迭代1。

在包含iframe的页面后面的代码中,我有:

 #region Web Methods
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static object GetUploadStatus()
{
    string fileNumber = HttpContext.Current.Session["FN"].ToString();
    return new
    {
        fileNumber = fileNumber
    };
}
#endregion

所以,过程是:我点击包含页面-上传器上的"尝试"跨度。Aspx -这调用javascript函数doupload()

这将在iframe的页面上提交表单。当此表单提交时,将运行增加Session["FN"]变量值的循环。同时,PageMethods。GetUploadStatus应该每500毫秒查询Session["FN"]的值。

BUT,唯一一次在PageMethods中发出警告。显示GetUploadStatus,在循环的所有迭代之后。它可以检索Session["FN"]的值,但只有一次,并且只有在iframe中的页面完成处理之后。

我一直读到我应该能够做我想做的事…任何帮助,非常感谢。我快疯了。抱歉,文章太长了

从iframe中的页面检索会话的PageMethods不起作用

asp.net会话状态以一种特殊的方式处理对同一会话的并发请求:

如果对同一个会话发出两个并发请求(通过使用相同的SessionID值),则第一个请求获得对会话信息。第二个请求只在第一个请求之后执行请求完成。

以上是来自MSDN的一篇文章。

你可以创建自己的SessionStateProvider,以不同的方式处理并发。