Posts Tagged ‘ postback ’

ASP.NET Postback Class Members


Basically I had a class variable in an ASP.NET and found it was getting lost on page post-back.. well all you have to do is put that object in the Viewstate object already created for you.

public partial class MyPage : System.Web.UI.Page
{
        protected int MyInt;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
		MyInt = 12345;
                ViewState["MyKey"] = MyInt;
            }
        }

        /*
	 This is called on a page post-back...
	*/
	protected void Event(object sender, EventArgs e)
        {
			//Get factor list from viewstate
			MyInt = (int)ViewState["MyKey"];

			//Go nuts
	}
}
Advertisements