views:

18

answers:

2

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

A: 

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

adrianos
Well, I'm actually storing the "datasource" used by this control in the ApplicationCache. The problem is that I am trying to uniquely identify that datasource in the cache so that the control can get it. Currently the cache Key is made up of the ID of the control, the sessionID, and then a name for the datasource (there are two, "current" and "original"). This is alright so long as the page on which the control exists only has one of these controls on it, but if there is a second one, they end up referencing the same datasource. So I was trying to add a GUID to the cachekey.
Amanda Myer
A: 

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.

Andrew Barber
I am using the cache to be able to reset the data back to it's original state when the user click on a "reset" button within the composite control. That's the primary purpose for it anyway.. I'm testing trying to move the code that assigns the datasource into the init event.
Amanda Myer
Okay; that should work as long as you don't have anything else happening in strange times that is dependent on it happening by a certain time. Generally, Data Binding is best done in the Load event (Page or Control), but I suggested at least Init to reduce the chance of hitting another issue if something depends on other things you are doing at the same time.
Andrew Barber
And I'd rather not clutter up this Question any more with discussion about what you are storing in Cache/Application; but there may also be a better way to handle that, depending on what you are storing.
Andrew Barber
I am interested in your thoughts about a better way to handle the data. I moved it to page_load, and it is.. kind of working. I might have some other bug, but in the debugger it shows that the cacheKey is no longer changing all the time (a good thing) but when I have multiples of this composite control on one page, they still seem to be mixing up their data occasionally.URGH.
Amanda Myer
It would probably be better to do that in a separate question; I would want to see the code that is binding the data, saving the key, and then re-loading the key and cached info. And when you say mixing up... do you mean displaying each others' data erroneously, or just all displaying the same set of data erroneously, or something else... that sort of thing :)
Andrew Barber