?
Solved

Persistent values in c#.NET

Posted on 2012-04-11
1
Medium Priority
?
294 Views
Last Modified: 2012-04-11
Hi,

I am trying to implement a page where, when you press a button, the value of an object field changes persistently.  For some reason, it does not do so now.  I have a feeling I'm missing something obvious.  

On the page, the object Test gets instantiated. The default value for the field mystring is "Init".  When the user presses the first button, the value gets updated to "new string". When the user presses the second button, the value reverts back to the default, which is NOT what I want to happen.  The value should stay the same.  What am I doing wrong?

Here's the code:

public class Test
{
    public string mystring = "Init";
}

public partial class TestPage : System.Web.UI.Page
{

    Test myTest=new Test();

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    public void Button1_Click(object sender, EventArgs e)
    {
       myTest.mystring="new string";
        Label1.Text = myTest.mystring;
    }

    public void Button2_Click(object sender, EventArgs e)
    {
        Label1.Text = myTest.mystring;
    }
}
0
Comment
Question by:EffinGood
[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
1 Comment
 
LVL 8

Accepted Solution

by:
PagodNaUtak earned 2000 total points
ID: 37835552
The reason why the string reverts to the original value is because all objects in ASP.Net Webapplication is destroyed once the page is created. Please see this link for more details
http://msdn.microsoft.com/en-us/library/ms178472.aspx

So as a work around you can store the class in viewState then retrieve that value upon page load to intialize the class again.

Please see the below code:  


 [Serializable]
    public class Test
    {
        public string mystring = "Init";
    }

   public partial class TestPage : System.Web.UI.Page
    {
        Test myTest;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                myTest = new Test();
                ViewState["test"] = myTest;
            }
            else
            {
                myTest = (Test)ViewState["test"];
            }
        }
        public void Button1_Click(object sender, EventArgs e)
        {
           myTest.mystring = "new string";
           Label1.Text = myTest.mystring;
        }

        public void Button2_Click(object sender, EventArgs e)
        {
            Label1.Text = myTest.mystring;
        }
    }
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month14 days, 12 hours left to enroll

770 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