In Visual studios, is the class recalled every time the page refreshes? I have the following class – I want to add value to a variable every time a button is clicked;
public partial class _Default : System.Web.UI.Page
{
Random random = new Random();
int total;
int playerTotalValue;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ranPlayer_Click(object sender, EventArgs e)
{
int randomNumTwo = random.Next(1, 10);
playerTotalValue = playerTotalValue + randomNumTwo; //playerTotalValue gets reset to zero on every click
playerTotal.Text = playerTotalValue.ToString();
}
}
playerTotalValue gets resets to zero every time I click ‘ranPlayer’ button, or this is what I think happens.
HTTP is stateless. That means it will not retain the values in the variable like you do in windows form programming. So whenever you click on the button it executes the same way as it loaded in the initial page loading. But wait !. You have the value available in the text box. So you can read the value from there and store it in the variable.