0

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

4

3 回答 3

0

你试过Session吗?

您可以为一个特定用户在会话对象中存储您喜欢的任何内容,在回发之间维护值/对象。

如果您想在全球范围内而不是按服务器上存储,请尝试应用程序

于 2010-10-29T20:00:57.660 回答
0

在控制构建期间根本没有很多可用的状态信息,所以这可能很困难。是否有某些原因您不能将访问缓存信息的代码移动到控件的 Init 事件中?

我假设您不能使用 Session 因为存储的信息与该特定请求/回发有关。如果不是特定于该请求,则使用 Session 可能是一种可能性-但我认为您可能会在生命周期的早期尝试处理控制状态时遇到其他问题。


在看到您对另一个答案的评论后;您应该能够将检查缓存数据源的代码移动到控件的 Init 甚至 Load 事件中,以便状态可用。

另外,顺便说一句;你确定你真的需要缓存这些数据吗?这最终可能会占用大量服务器内存。

于 2010-10-29T20:01:08.273 回答
0

虽然这不是最好的解决方案(重新排列逻辑以适应生命周期模型通常是),但您是否尝试过直接访问请求?我曾经真的很想在生命周期的早期从 DropDownList 中获取选定的值,这样我就可以调整建筑物中的一些元素,我这样做了:

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

因此,我没有等待视图状态加载服务器端代理的值,而是自己从帖子中传递的客户端控制值中获取它。如果我重新设计那个页面,我可能会做不同的事情,但它现在似乎已经解决了,它解决了我遇到的问题。

于 2010-11-08T16:15:33.813 回答