Solved

Persistent values in c#.NET

Posted on 2012-04-11
1
286 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
1 Comment
 
LVL 8

Accepted Solution

by:
PagodNaUtak earned 500 total points
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

728 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

9 Experts available now in Live!

Get 1:1 Help Now