gpt4 book ai didi

c# - LoadControlState 没有被解雇

转载 作者:太空宇宙 更新时间:2023-11-03 11:41:31 25 4
gpt4 key购买 nike

我在使用自定义控件(继承自用户控件)时遇到问题 - 我的 LoadControlState 没有被触发。

嗯,准确地说:它正常触发,但是当我覆盖页面的 LoadPageStateFromPersistenceMedium 和 SavePageStateToPersistenceMedium 函数时,它不再被触发。

是否有任何我应该调查的导致 LoadControlState 未被触发的典型原因?它何时被解雇是否有任何先决条件?

谢谢

最佳答案

就其值(value)而言,以下是我如何覆盖 Save/LoadPageStateFromPersistenceMedium 函数。基本上,它将 View 状态存储在用户 session 中,以使回发更快:

    // Inspired by: http://aspalliance.com/72
const string ViewStateFieldName = "__VIEWSTATEKEY";
const string ViewStateKeyPrefix = "ViewState_";
const string RecentViewStateQueue = "ViewStateQueue";
const int RecentViewStateQueueMaxLength = 5;

protected override object LoadPageStateFromPersistenceMedium()
{
// The cache key for this viewstate is stored in a hidden field, so grab it
string viewStateKey = Request.Form[ViewStateFieldName] as string;
if (viewStateKey == null) return null;

// Grab the viewstate data using the key to look it up
return Session[viewStateKey];
}

protected override void SavePageStateToPersistenceMedium(object viewState)
{
// Give this viewstate a random key
string viewStateKey = ViewStateKeyPrefix + Guid.NewGuid().ToString();

// Store the viewstate
Session[viewStateKey] = viewState;

// Store the viewstate's key in a hidden field, so on postback we can grab it from the cache
ClientScript.RegisterHiddenField(ViewStateFieldName, viewStateKey);

// Some tidying up: keep track of the X most recent viewstates for this user, and remove old ones
var recent = Session[RecentViewStateQueue] as Queue<string>;
if (recent == null) Session[RecentViewStateQueue] = recent = new Queue<string>();
recent.Enqueue(viewStateKey); // Add this new one so it'll get removed later
while (recent.Count > RecentViewStateQueueMaxLength) // If we've got lots in the queue, remove the old ones
Session.Remove(recent.Dequeue());
}

关于c# - LoadControlState 没有被解雇,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4643964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com