Solved

ViewState not working across postbacks

Posted on 2010-09-22
7
713 Views
Last Modified: 2012-05-10
I am trying to set a value into ViewState but the value isn't saving across postbacks. (I am actually trying to save a seriazable class, but for simplicity I am testing using a simple string value which isn't saving either).

I have tried manually setting the EnabledViewState = true property in the page directive of the master page, and I am also testing to see whether ViewState is enabled at the start of the method, which it is - but still the value is being reset on every postback.

The OnPreRender occurs late enough in the Page Lifecycle so that isn't the issue.  

Any ideas?

protected override void OnPreRender(EventArgs e)
    {
        bool viewState = this.IsViewStateEnabled;

        if (!Page.IsPostBack)
        {
            if (ViewState["Test"] == null)
                ViewState["Test"] = "This is a test";

            string test = (string)ViewState["Test"];
        }
    }

Open in new window

0
Comment
Question by:markerasmus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
7 Comments
 
LVL 5

Assisted Solution

by:Siyamala
Siyamala earned 250 total points
ID: 33733278
try
change asp page - first line

AutoEventWireup="true" to false
0
 

Author Comment

by:markerasmus
ID: 33735129
Nope, that didn't work.
0
 
LVL 10

Accepted Solution

by:
codingbeaver earned 250 total points
ID: 33736016
My guess is it has something to do with when you set ViesState value. The order of firing events is important.
0
 

Author Comment

by:markerasmus
ID: 33736459
I've decided to use session state instead.
0
 
LVL 5

Expert Comment

by:Siyamala
ID: 33989220
Hi,

My answer answer has been accepted as a Assisted Solution ,But still the score Points is not Updated.

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled

739 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