How can I get page variable values to persist accross postbacks?

Posted on 2005-05-16
Last Modified: 2010-05-18
I heard a developer tell me page variables persisted across postbacks, but I haven’t ever seen this work, can it?

so if I have a page like so:

namespace blah
      public class page : System.Web.UI.Page
            protected int imageID;

            private void Page_Load(object sender, System.EventArgs e)
                  if (!IsPostBack) imageID = 5;

how can I get the imageID property to keep it's value on a postback?

Question by:brooklynDev
    LVL 13

    Expert Comment

    Try a bit of Viewstate:
    or add a hidden literal/label/field to the page
    LVL 9

    Expert Comment

    softplus is correct; throw that variable in the ViewState bag and grab it in your postback.  Something kind of like this:

    private void Page_Load(object sender, System.EventArgs e)
       if(!this.IsPostBack || ViewState["imageID"] == null)
          this.imageID = 5;
          ViewState["imageID"] = this.imageID;
          this.imageID = (int)ViewState["imageID"];

    Author Comment

    so I take it that dev I was talking about was wrong, and page variables as illustrated in my question can and do not persist across postbacks
    LVL 9

    Accepted Solution

    That's correct; imageID, in the code you posted, will not retain its value across postbacks.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
    Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now