D
Deckarep
Hey all, this question is somewhat related to ASP.NET but I think it's
very relevant to how the dynamics of the C# language work so I'm
asking it in this forum:
In my webcontrol class that inherits from System.Web.UI.UserControl.
I have a private static variable declared. And on my OnLoad I
increment this int type value by one.
Now, I have five of these instance webcontrols embeded on the same
page each with their own different html.
Now when I hit the page with my browser I'm expecting to see that
static "counter" variable set to 5. Because each of those controls
increments the static variable by one.
Now when I hit the page again on postback that value is now 10....then
another postback to 20....etc...etc.
WTF? Why is this happening? When the webcontrols get unloaded
by .Net should the objects be destroyed and garbage collected at one
point including all members belonging to that object? But no, this
value is persisting across postbacks.
This seams like incorrect behevior on the part of the CLR or
something. If there is a reason why these values are persisting
across postbacks or some MSDN article thouroughly explaining this
beheavior I'd like to be pointed to the URL because this is the first
time I've realized this and frankly it bothers me.
By the way I've already searched google about this and found different
forums of people explaining different reasons which have been
conflicting...which is why I'm asking here to set the record straight.
Here is some sample code...nothing complex here:
public class MarketingUserControl : System.Web.UI.UserControl
{
private static int m_iTourCount = 0; //<---- Why isn't this
resetting this variable across postbacks?
protected override void OnLoad( EventArgs e )
{
base.OnLoad( e );
m_iTourCount++; // <----- Why does this guy persist
across postbacks?
}
}
very relevant to how the dynamics of the C# language work so I'm
asking it in this forum:
In my webcontrol class that inherits from System.Web.UI.UserControl.
I have a private static variable declared. And on my OnLoad I
increment this int type value by one.
Now, I have five of these instance webcontrols embeded on the same
page each with their own different html.
Now when I hit the page with my browser I'm expecting to see that
static "counter" variable set to 5. Because each of those controls
increments the static variable by one.
Now when I hit the page again on postback that value is now 10....then
another postback to 20....etc...etc.
WTF? Why is this happening? When the webcontrols get unloaded
by .Net should the objects be destroyed and garbage collected at one
point including all members belonging to that object? But no, this
value is persisting across postbacks.
This seams like incorrect behevior on the part of the CLR or
something. If there is a reason why these values are persisting
across postbacks or some MSDN article thouroughly explaining this
beheavior I'd like to be pointed to the URL because this is the first
time I've realized this and frankly it bothers me.
By the way I've already searched google about this and found different
forums of people explaining different reasons which have been
conflicting...which is why I'm asking here to set the record straight.
Here is some sample code...nothing complex here:
public class MarketingUserControl : System.Web.UI.UserControl
{
private static int m_iTourCount = 0; //<---- Why isn't this
resetting this variable across postbacks?
protected override void OnLoad( EventArgs e )
{
base.OnLoad( e );
m_iTourCount++; // <----- Why does this guy persist
across postbacks?
}
}