Solved

Persistent values in c#.NET

Posted on 2012-04-11
1
293 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 500 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

688 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