We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

variables disappear on postback

cberinger
cberinger asked
on
Medium Priority
840 Views
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.

.aspx
<asp:DropDownList AutoPostBack="true" OnSelectedIndexChanged="MainMenuID_SelectedIndexChanged" ID="MainMenuID" runat="server"></asp:DropDownList>
 
.cs
   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

Comment
Watch Question

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.
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.

Not the solution you were looking for? Getting a personalized solution is easy.

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

Author

Commented:
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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.