我沒有維護線程或會話,但是由於創建了一個新線程,定義的變量在回發期間丟失。思考?ASP.NET Postback創建一個新線程和一個新會話
每次單擊button1或button2時,currentthreadid和sessionid都會更改。我更關心正在創建的新線程。請幫助 !
這裏是我的WebForm1.aspx的代碼
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="UpdatePanelTest2.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" EnablePartialRendering="true" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Before Click" />
<asp:Button ID="Button1" runat="server" Text="Button1" OnClick="Button1_Click" CausesValidation="false"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<asp:Button ID="Button2" runat="server" Text="Button2" OnClick="Button2_Click"/>
</form>
</body>
</html>
這裏是我的WebForm1.aspx.cs中的代碼
namespace UpdatePanelTest2
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string currentthreadid = AppDomain.GetCurrentThreadId().ToString();
string sessionid = Session.SessionID;
if (IsPostBack) { }
if (IsAsync) { }
if (IsPostBackEventControlRegistered) { }
if (ScriptManager1.IsInAsyncPostBack) { }
}
protected void Page_Init(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
this.Label1.Text = "After Click";
}
protected void Button2_Click(object sender, EventArgs e)
{
}
}
}
我的web.config文件
<?xml version="1.0"?>
<!-- For more information on how to configure your ASP.NET application, please visit http://go.microsoft.com/fwlink/?LinkId=169433 -->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
</configuration>
UPDATE
的原因,我需要的是同一個線程用於回傳要求,因爲我保持一定的變量,然後再利用這些變量對當前池線程
代碼:
private static readonly ThreadLocal<PageContext> _context = new ThreadLocal<PageContext>(typeof(PageContext), "_context", true);
private string[] _path;
public static PageContext ForCurrentThread
{
get { return _context.Value ?? (_context.Value = new PageContext()); }
}
public void Initialize(HttpRequest _request)
{
somestring = null;
}
public string somestring { get; set; }
當頁面加載第一次,somestring被賦予一個值,其他的類然後使用相同的上下文並重新使用somestring的值。如果爲回發請求創建了新線程,這不起作用。
無法理解您的問題。您只想存儲一次值? –
還有一個類似的線程 - 請按照下面的鏈接。 http://stackoverflow.com/questions/222999/how-to-persist-variable-on-postback – meghlashomoy
你想保持什麼變量? – smoksnes