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
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: