title: ASP.NET / UserControl, ViewState and losing Property values
body: <p>this is about ASP.NET, ViewState, UserControls and losing the values of my properties. It is an classic question by know, I know, and even though I have searched here and on Google for a resolution to this problem, I havent succeeded. On the contrary, the more I test different things, the less I understand it seems.</p>
  
  <p>On top of this I am using ext.net and their so called DirectMethod's but I dont think that has much to do with this problem. There are numerous questions I have and I hope that this text will be fairly readable and understandable =)</p>
  
  <p><strong>The UserControl</strong></p>
  
  <p>I have a UserControl, Customers.ascx, that contains some Properties. The "most" important is *_CustomerId*. The _CustomerId is set in code-behind, in ext.net's "DirectMethod" like this (code below from the Page Customers.aspx):</p>
  
  <pre><code>    [DirectMethod]
      public void SetCustomer()
      {
          RowSelectionModel sm = GridPanel2.SelectionModel.Primary as RowSelectionModel;
  
          if (sm.SelectedRow != null)
          {
              uint customerId = uint.Parse(sm.SelectedRow.RecordID);
              customer_modify._CustomerId = customerId;
          }
      }
  </code></pre>
  
  <p>The "customer_modify" is the instance of the UserControl as defined in Customers.aspx (a Page, not a UserControl):</p>
  
  <pre><code>&lt;asp:Content ID="Content2" ContentPlaceHolderID="CPH_center" runat="server"&gt;
      &lt;CP:Customer ID="customer_modify" runat="server" _IsCreateMode="false" ViewStateMode="Enabled" /&gt;   
  &lt;/asp:Content&gt;
  </code></pre>
  
  <p>In that Page, Customers.aspx, I have ext.net GridPanel that lists the Customers and when I click the rows in the GridPanel the (Direct)method SetCustomer is executed.</p>
  
  <p>As you can see in the <em>SetCustomer</em>-method, the property "_CustomerId" is updated with an uint, so lets take a look at the Property in the UserControl:</p>
  
  <pre><code>    public uint _CustomerId 
      { 
          get
          {
              object o = ViewState["_CustomerId"];
              if (o == null)
                  return 0;
              return (uint)o;
          }
          set
          {
              object o = ViewState["_CustomerId"];
              ViewState["_CustomerId"] = value;
              SetCustomer();
          }
      }
  </code></pre>
  
  <p>As you can see, I am using the ViewState-thingie and it was my hope that the state, ie the value of the properties would be remembered. They are not.</p>
  
  <p>When I click a Row in the GridPanel <em>the first time</em> I can see (by breaking) that the ViewState["_CustomerId"] == null which is alright. I see thereafter that the setter for _CustomerId is executed and that the ViewState["_CustomerId"] is assigned the RecordID (uint).</p>
  
  <p>I now click another Row in the GridPanel and the SetCustomer-method is executed again. I break in the setter for _CustomerId and take a look at what the ViewState["_CustomerId"] is BEFORE assigning it. <strong>It is 0, but I expect it to be the value from the previous Row clicked.</strong></p>
  
  <p>Why is it 0? What am I missing?</p>
  
  <p><strong>Also:</strong></p>
  
  <p>I actually have two of there UserControls on the Page, the other one named "customer_create":</p>
  
  <pre><code>&lt;ext:Window 
          ID="Window_CreateNewCustomer" 
          runat="server" 
          Icon="New"
          Title="Skapa ny kund" 
          Hidden="true"
          Width="480"
          Height="370"
          Modal="true"&gt;
  
          &lt;Content&gt;
              &lt;CP:Customer ID="customer_create" runat="server" Title="Skapa ny kund" _IsCreateMode="true" /&gt;
          &lt;/Content&gt;
  &lt;/ext:Window&gt;
  </code></pre>
  
  <p>What disturbs me a lot is that each and every time I click on a Row in the GridPanel the Page_Load in the UserControl <em>is executed twice</em>, even though I am only updating one of them in the SetCustomer()-method.</p>
  
  <p>Why is that?</p>
  
  <p>Any tips, ideas and help would be greatly appreciated.</p>