私はスレッドやセッションを維持していませんが、新しいスレッドが作成されるので、定義された変数はポストバック中に失われます。思考?ASP.NETポストバックは新しいスレッドと新しいセッションを作成します
currentthreadidとsessionidは、button1またはbutton2のいずれかをクリックするたびに変更されます。私は、新しいスレッドが作成されるのをもっと心配しています。助けてください !ここでは、ここで
私の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値を再利用します。ポストバック要求のために新しいスレッドが作成された場合、これは機能しません。
あなたの質問を理解できません。あなたは値を1回だけ保存したいですか? –
似たようなスレッドがあります。以下のリンクに従ってください。 http://stackoverflow.com/questions/222999/how-to-persist-variable-on-postback – meghlashomoy
同じ変数をどのように変えたいですか? – smoksnes