Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Persistent values in c#.NET

Posted on 2012-04-11
1
Medium Priority
?
295 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

604 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