2013-03-07 26 views
2

2页。 Uploader.aspx和Uploadee.aspx。PageMethods从iframe中的页面检索会话不工作

Uploadee.aspx位于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); 
      } 
     } 
    } 
} 

所以,我在一个循环增加会话变量 - 会议[“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 

因此,其过程是:我点击了包含页面上的“试试看”跨度 - Uploader.aspx - 并调用javascript函数doupload()

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

但是,唯一一次显示PageMethods.GetUploadStatus中的警报,是在循环的所有迭代之后。它检索Session [“FN”]的值 - 好吧 - 但只有一次,并且仅在iframe中的页面完成处理后。

我一直在阅读,我应该能够做我想做的事......任何帮助非常感激。这让我疯狂。道歉的帖子的长度。

回答

0

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

如果两个并发请求为同一会议上提出的(通过使用 相同的SessionID值),第一请求获得对会话信息的独占访问权限。第二个请求仅在第一个请求完成后才会执行。

以上是从MSDN article

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

+0

谢谢您的回复。任何想法为什么/如何工作... http://www.codeproject.com/Articles/113418/ASP-NET-File-Upload-with-Progress-Bar – 2013-03-07 17:16:25

+0

得到它的工作,通过添加EnableSessionState =“ReadOnly” Async =“true”到iframe中的页面。 不明白...你会认为EnableSessionState =“ReadOnly”意味着你只能从Session中读取......但是我正在循环中写入Session,现在,包含iframe的页面可以访问会话变量值随着变化而变化。奇怪 - 但仍然有效。 – 2013-03-07 19:44:07