I have created a button that will load a page that contains a PDF document that users can fill out and save. After the save is clicked, the page posts-back and there is code to save the changes. However, I'm not able to get the session to preserve the value after postback. I have it enabled on the site and in the web.config. When the button is initially clicked and the page with the PDF is loaded, the ItemId gets pulled from the query string and stored in the session state variable. The next the page posts-back is when the user clicks the button to save the PDF, but at this point, the session state gets reset and even gets assigned a new unique identifier. Does anyone know a way to prevent this from happening?

 protected void Page_Load(object sender, EventArgs e)
        itemId = Request.QueryString["ItemId"];

        if (Session["ItemId"] == null || Session["ItemId"] != itemId){
            //HttpContext.Current.Session["ItemId"] = itemId;
            Session["ItemId"] = itemId;

        if (!PostBackFromPDF())
            //-- Generate the pdf or display pre-existing message
            string savedValue = string.Empty;

            if (Session["ItemId"] != null)
                savedValue = Session["ItemId"].ToString();
                itemId = savedValue;

            //-- Post Back From PDF Form


Thanks in advnce


Ever thought of using the viewstate to store the itemid instead of the session state?

  • I tried that and got the same result. – Music Magi Feb 25 '13 at 19:20
  • So you are on the same page, and from page load to a post back the value stored in the viewstate didn't come back? – Steve Lineberry Feb 25 '13 at 19:22
  • Do you have viewstate enabled on your page? – Steve Lineberry Feb 25 '13 at 19:25
  • <%@Page EnableViewState="True" %> – Steve Lineberry Feb 25 '13 at 19:38

I did get this working, but it looks like it wasn't really a problem of session. We ended up setting the session in a different page and it worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.