[英]Weird behavior of UpdatePanel with Session vs. ViewState
早上好,
這更像是WTF的類型問題而不是其他任何問題。
我的應用程序正在使用ViewState 。 我在我的應用程序上放置了一個UpdatePanel ,僅用於測試目的。 它所做的只是每秒點擊我的日志表,並為我生成一個實時日志控制台,以查看應用程序如何處理事物。
今天早上我決定將ViewState的使用轉換為Session 。 現在我的UpdatePanel不再同步運行並“暫停”直到操作完成以更新UpdatePanel / Console。
切換回ViewState會使事情正常運行......
此外,我的UpdatePanel正在調用的函數與ViewState或SessionState無關,也沒有調用...
我已經能夠在小測試樣本中重現此行為:
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="TestUpdatePanelWithSession.master.cs" Inherits="Tester2._0.TestUpdatePanelWithSession1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:Timer ID="Timer1" runat="server" Enabled="True" Interval="1000"></asp:Timer>
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
<asp:UpdatePanel ID="UpdatePanel1"
UpdateMode="Always"
runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
<%= System.DateTime.Now.ToString("mm:ss") %>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
<%@ Page Title="" Language="C#" MasterPageFile="~/TestUpdatePanelWithSession.Master" AutoEventWireup="true" CodeBehind="TestUpdatePanelWithSession.aspx.cs" Inherits="Tester2._0.TestUpdatePanelWithSession" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:Label ID="lblTest" runat="server" Text="Label"></asp:Label>
<asp:Button ID="btnStartTest" runat="server" Text="Test" OnClick="btnStartTest_Click" />
</asp:Content>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Tester2._0
{
public partial class TestUpdatePanelWithSession : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Session["SomeIndex"] = 50000;
}
}
protected void btnStartTest_Click(object sender, EventArgs e)
{
for (int i = 0; i < 1000000; i++)
{
System.Threading.Thread.Sleep(500);
Session["SomeOtherIndex"] = i;
lblTest.Text = ((int)Session["SomeIndex"] + (int)Session["SomeOtherIndex"]).ToString();
}
}
}
}
如果您將所有Session更改為ViewState,上面的代碼可以正常工作。 到底他媽發生了什么??
提前感謝上課...
在測試該示例之后,很明顯問題來自會話的讀寫鎖定。
從Page_Load中刪除會話使用可以避免這種行為,但這有點奇怪,因為當updatepanel調用頁面時沒有命中代碼。 即使將代碼移動到其他函數,在另一個線程上調用它或使用任務也不會阻止此行為。
似乎ASP .net正在分析代碼,當有代碼使用會話時,即使它沒有被執行,它也會阻止會話阻止正常執行回調。 只留下控制事件的會話操作似乎工作正常。
我測試的只有解決方案是刪除Page_Load會話操作,在某些情況下可能是不可避免的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.