[英]PageMethods to retrieve session from page in iframe not working
2頁。 Uploader.aspx和Uploadee.aspx。
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);
}
}
}
}
因此,我處於一個循環中,每次迭代將會話變量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
因此,過程是:我單擊包含頁面上的“ Try it”跨度-Uploader.aspx-這將調用javascript函數doupload()
這會將表單提交到iframe中的頁面上。 提交此表單后,將運行循環,以增加Session [“ FN”]變量的值。 同時,PageMethods.GetUploadStatus應該每500毫秒查詢一次Session [“ FN”]的值。
但是,只有在循環的所有迭代之后,才顯示PageMethods.GetUploadStatus中的警報。 它可以正常檢索Session [“ FN”]的值-但只有一次並且僅在iframe中的頁面完成處理之后。
我一直在讀,我應該能夠做我想做的事情……任何幫助,我將不勝感激。 這讓我發瘋。 對於帖子的長度,我們深表歉意。
asp.net會話狀態以特殊方式處理對同一會話的並發請求:
如果對同一會話發出兩個並發請求(通過使用相同的SessionID值),則第一個請求將獲得對該會話信息的互斥訪問。 僅在第一個請求完成后才執行第二個請求。
以上摘自MSDN文章 。
您可以創建自己的以不同方式處理並發性的SessionStateProvider 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.