I am not maintaining threads or session, however the variables defined are lost during a postback because a new thread is created. Thoughts?
currentthreadid and sessionid change every time when either button1 or button2 are clicked. I am more concerned about new threads being created. Help please !
here is my WebForm1.aspx code
<%@ 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>
here is my WebForm1.aspx.cs code
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)
{
}
}
}
my web.config file
<?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
The reason i need it to be the same thread for postback calls because i maintain certain variables and then re-use those variables for the current pool thread
code:
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; }
When the page loads the first time, somestring is given a value, other classes then use the same context and re-use that somestring value. This doesnt work if a new thread is created for postback requests.