title: web user control persisting properties (viewstate, session, context) Am I missing something
body: <p>Maybe someone can help me out. I have created a simple web user control, a date and time picker, to be dropped onto my webform. This all works very nicely, I can set properties, usee the control satisfactorily for ui etc.</p>
  
  <p>When it comes time to "use" the controls selected_date_time property, I just cant get it to persist. Nothing.. I have researched and tried endlessly using viewsate, context and session. Onbiouosly session works, but its dirty, and I am using two copies of this control (start and end time), so session vars really needs to be hacked to maket his work.</p>
  
  <p>Am I missing something? The control gets initialized every time something happens, and obviuosly loses its state information. The ui keeps its state tho, as I can select a date, write that date to a label, and that persists. But when I try to access my properties of the control to retreive the selected combined date and time, (that is already persisting visually), its nothing. I debuggeed and it gets initialized everytime I do any form of post on the page.</p>
  
  <p>Can someone please shed some light on this for me? Its really starting to be an issue now.</p>
  
  <p>Thanks in advance.</p>
  
  <p>Example: (simple components)</p>
  
  <p>UC _ save_method</p>
  
  <pre><code>ViewState("var_time") = "My veiwstate text"
  </code></pre>
  
  <p>form _read_method</p>
  
  <pre><code>dim str as string = ViewState("var_time")
  </code></pre>
  
  <p>form sees nothing in the viewstate var.</p>
  
  <p>I have also tried it with normal properties and values, which wasn't working, which is why I moved on to viewsate var's for my properties. Right now, I am just trying to get the viewstate to work even without properties.</p>
  
  <p>Seems my form and the control must have two seperate viewstates? I am a bit of a n00b concerning viewstates.</p>
  
  <p>Thanks</p>
  
  <p>[solution]
  You have to explicitly reset your properties on the prerender method of the control. My misunderstanding was that the page and the control share the same single viewstate. Turns out the control and the page that its on, has its own independant viewstates.</p>
  
  <p>So absically, on your property set function in the control, set your value in the viewstate, and upon prerender, take that viewstate value, and set the property get variable =- the value in viewstate.</p>
  
  <p>You can now access the property from your page as if everything was normal and the world is not ending... <em>pheww</em></p>
  
  <p>Thanks to cnay for directing me.</p>
  
  <p><code>
     'Usr control xyz</p>
  
  <p>Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender</p>
  
  <p>my_time = ViewState("var_time")
     'my_time is the get variable for property date_time</p>
  
  <p>End Sub</p>
  
  <p>'page use</p>
  
  <p>xyz.date_time
  </code></p>