我没有维护线程或会话,但是由于创建了一个新线程,定义的变量在回发期间丢失。思考?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