The variables in the partial class _Default get reset in the function call as mentioned in the code below. But, on marking the properties as static, their values are retained.
My questions is, why does this happen? Doesn’t each function call use same instance of page class?
public partial class _Default : System.Web.UI.Page
{
public double ValueToConvert { get; set; }
public double ConvertedValue { get; set; }
protected void Page_Load(object sender, EventArgs e){}
protected void btnUC_Click(object sender, EventArgs e)
{
//In this method, the non-static properties ValueToConvert and ConvertedValue
//get reset. But why?
}
}
Well each request will create a new instance of the class. Heck, they could be on different processes or even different machines. If you have multiple method calls within the same request that will use the same instance, but otherwise you need to work out how you expect the state to be propagated. You could propagate it via the client (viewstate) or store it somewhere server-side (e.g. in a database).