In my web application I have two, a ViewState and a Session which hold values, the problem is that I need to reset one and leave the other as it is through a click of a button. If I use Response.Redirect in my button, both the ViewState and the Session are resetted. I tried using if(!IsPostBack) but I don’t think this would work in a button event. I would really appreciate your suggestions and help.
CODE:
// There is a ViewState above this code which I have to reset
protected void Button_Click(object sender, EventArgs e)
{
Session["Counter"] = (int)Session["Counter"] + 1; // I do not want to reset this Session.
Label1.Text = Session["Counter"].ToString();
Response.Redirect("Page1.aspx"); // If this button is pressed then Session["counter"] is resetted which I don't want to happen
}
Thanks !!
If you simply want to incremement the counter, all you need to do is this:
Since the page already saves state, the label will post back to the server with it’s current value restored from view state. You simply pull the value, and then set the value with your modifications. Try that, and it should work.
Your solution is actually overcomplicating things.