[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

variables disappear on postback

Posted on 2009-02-12
4
Medium Priority
?
832 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

0
Comment
Question by:cberinger
  • 2
4 Comments
 
LVL 11

Expert Comment

by:jmwheeler
ID: 23623779
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.
0
 
LVL 22

Accepted Solution

by:
prairiedog earned 2000 total points
ID: 23623782
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.
0
 
LVL 11

Expert Comment

by:jmwheeler
ID: 23623822
Actually myTitle should still have a value since it is being set in the Page_Load event every time.
0
 

Author Comment

by:cberinger
ID: 23627613
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.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Loops Section Overview
Suggested Courses
Course of the Month18 days, 14 hours left to enroll

834 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