variables disappear on postback

Posted on 2009-02-12
Last Modified: 2013-12-17
I have some code that generates a title from a database via Page.Header.Title.  This works fine when the page loads, but the title disappears when I fire a postback event.  Anyone have any suggestions around this issue?
Thanks for reading.


<asp:DropDownList AutoPostBack="true" OnSelectedIndexChanged="MainMenuID_SelectedIndexChanged" ID="MainMenuID" runat="server"></asp:DropDownList>


   public string myTitle;

   protected void Page_Load(object sender, EventArgs e)


      //myTitle variable is populated from a database...  

      myTitle = "Edit: " + DR["title"].ToString();

      Page.Header.Title = myTitle;


   protected void MainMenuID_SelectedIndexChanged(object sender, EventArgs e)


      //this code populates another menu, but does not touch Page.Header.Title or the myTitle variable

      //This next line populates the title with a blank title...

      Page.Header.Title = myTitle;


Open in new window

Question by:cberinger
    LVL 11

    Expert Comment

    You should not have to set the title inside MainMenuID_SelectedIndexChanged because Page_Load runs for every postback.

    If removing that line doesn't work then post some more code.  I'm assuming DR is a datarow but I don't know how it got a value in the first place.
    LVL 22

    Accepted Solution

    You will need to save myTitle in a ViewState or Session variable. Upon postback,  myTitle in your code will be redefined and that is why the data is lost.
    LVL 11

    Expert Comment

    Actually myTitle should still have a value since it is being set in the Page_Load event every time.

    Author Comment

    I tried a Session variable, but it kept timing out.  This was not desired in my application.  ViewState was perfect.
    ViewState["myTitle"] = "Edit: " + DR["title"].ToString();
    Page.Header.Title = ViewState["myTitle"].ToString();

    Thank you for your help.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    759 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

    13 Experts available now in Live!

    Get 1:1 Help Now