title: C# ASP.NET Logging to file with help of ViewState
body: <p>On my page I have this button that triggers an event and logs to a txt file.</p>
  
  <pre><code>void ButtonClick(object sender, EventArgs e)
      {
          // Stop/start service
          var button = (Button)sender;
          string machine = button.CommandArgument;
          string service;
  
          /*Do stuff */
  
          if (buttonClicked)
          {
              writeToLogFile("User " + HttpContext.Current.User.Identity.Name + " pressed the " + button.CommandName + " button on server " + machine + " with the services " + checkedBoxes(machine) + " checked.");
              ViewState["buttonClicked"] = buttonClicked;
          }
      }
  </code></pre>
  
  <p>The button changes the state of my page. This state can also be change from other sources, so therefore I log that too. </p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
      {
          if (!IsPostBack)
          {
              prevStatus = new Dictionary&lt;string, ServiceControllerStatus&gt;();
              currStatus = new Dictionary&lt;string, ServiceControllerStatus&gt;();
              buttonClicked = false;
              ViewState["buttonClicked"] = false;
          }
          else
          {
              prevStatus = (Dictionary&lt;string, ServiceControllerStatus&gt;)ViewState["prevStatus"];
              currStatus = (Dictionary&lt;string, ServiceControllerStatus&gt;)ViewState["currStatus"];
              buttonClicked = (bool)ViewState["buttonClicked"];
              if (!buttonClicked)
              {
                  foreach (var item in currStatus)
                  {
                      ServiceControllerStatus oldStatus;
                      if (prevStatus.TryGetValue(item.Key, out oldStatus) &amp;&amp; !(oldStatus == item.Value))
                      {
                          string[] split = item.Key.Split('_');
                          writeToLogFile("The service " + split[0] + " on server " + split[1] + " was changed to " + item.Value + " from another source.");
                      }
                  }
              }
              else
              {
                  ViewState["buttonClicked"] = false;
              }
  </code></pre>
  
  <p>But this log also logs when I click the button, which i don't want. Therefore I have to keep the state variable <code>buttonClicked</code> between postbacks. </p>
  
  <p>The issue at hand here is the last else case where I reset the ViewState Variable. Everything else works fine. But if I don't have this reset, after I click the button the other logger stops logging since <code>buttonClicked</code> stays true. But if I have the reset implemented the ViewState gets set to false at the start of the Page_Load and therefore loses all meaning since it then logs twice, one for the button and one for the other logger. I've been pulling my hair trying to figure out why this else case seems to happen before the if statement.</p>
  
  <p>If I debug and step from a button click event as soon as I hit Page_Load the ViewState is set to false. If I comment out the line within the else case it is not set to false while stepping through debugging. What is going on?</p>