Louis van Tonder
Louis van Tonder

Reputation: 3700

web user control persisting properties (viewstate, session, context) Am I missing something

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.

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.

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.

Can someone please shed some light on this for me? Its really starting to be an issue now.

Thanks in advance.

Example: (simple components)

UC _ save_method

ViewState("var_time") = "My veiwstate text"

form _read_method

dim str as string = ViewState("var_time")

form sees nothing in the viewstate var.

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.

Seems my form and the control must have two seperate viewstates? I am a bit of a n00b concerning viewstates.

Thanks

[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.

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.

You can now access the property from your page as if everything was normal and the world is not ending... pheww

Thanks to cnay for directing me.

'Usr control xyz

Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

my_time = ViewState("var_time") 'my_time is the get variable for property date_time

End Sub

'page use

xyz.date_time

Upvotes: 1

Views: 2552

Answers (1)

VinayC
VinayC

Reputation: 49195

Perhaps, you should post the complete control code to get the better answer but speaking in general terms - view-state (or control state) is a typically correct approach to persists control state such as properties. Each control instance get the different view-state bag and hence collisions are easily avoided.

Now, coming to your problem, a typical editable control first restores its state from view-state and then uses the request data to modify the state as needed. For example, a simple text-box control would persists its text value in view-state. On post-back, the text value will be restored from the view-state and then gets overwritten by the value present in the request (looked up by UniqueID property).

Now, for user controls, typically you can use child controls values (or properties) to derive the control value/properties - so these may not use view-state. However, if you add properties/state that is not backup by child controls then you may have to back them up in the view-state. So let's say your user control has two child controls - one for Date and other for time then can combine their values to get the selected date-time value for your control.

Upvotes: 2

Related Questions