Question

I have a composite control that has a couple of private fields that reference values in the cache and these private fields are called during the constructor method. Since a string key is used to identify the value in the cache, I must have a way of storing that string key in such a way that it is available at the time the control is instantiated, and I have to be able to reference it on postbacks without it changing.

In addition, this key is generated the first time the control is loaded, but it should not be changed again after that first time.

How can I accomplish this?

I have already tried saving it to viewstate, but that doesn't work because viewstate is not yet available at the time the control is instantiated.

I have tried using a private field and then checking against Page.IsPostback in the constructor and if it isn't postback, I assign a value to the private field, but on subsequent postbacks it looses it's value, and I can't reassign it in the Page.IsPostBack again because it is an autogenerated GUID.

This has got to be something folks have had to do before....

Was it helpful?

Solution

There isn't a lot of state info available during control construction at all, so this could be difficult. Is there some reason you can't move your code which accesses the Cache'ed info into the control's Init event?

I assume you can't use Session because the information stored is related to that specific request/postback. If it's not specific to that request, using Session could be a possibility - but I think you may encounter other problems trying to deal with control state so early in the lifetime.


After seeing your comment to the other answer; you should be able to move your code that checks for the cached datasource into the control's Init or even Load event, so the state will be available.

Also, incidentally; are you sure you really need to cache this data? That could end up taking up a lot of server memory.

OTHER TIPS

Have you tried Session?

You can store anything you like in the session object for one particular user, maintaining the value / object between postbacks.

If you want to store on a global basis and not per ser basis, try Application

Although this isn't the best solution (rearranging your logic to fit the lifecycle model generally is), have you tried accessing the Request directly? I once really wanted to get the selected value off a DropDownList very early in the lifecycle so I could adjust some elements in the building, and I did it like this:

myDropDownList.SelectedValue = Page.Request.Form[myDropDownList.UniqueID];

So instead of waiting for the viewstate to load the server-side proxie's values, I just got it myself from the client-side control value that was passed in on the post. I probably would do things differently if I redesigned that page, but it seems to have worked out alright for now and it solved the problem I was having.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top