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
Solved

Persistent values in c#.NET

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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