Asp.net 保存控件';缓存中的s(ASP面板和子级)状态

Asp.net 保存控件';缓存中的s(ASP面板和子级)状态,asp.net,caching,viewstate,Asp.net,Caching,Viewstate,我正在寻找一种简单且可扩展的方法来为用户缓存过滤器设置。我最初的想法是覆盖包含过滤器控件的面板的保存/加载ViewState进程,并在那里保存/加载控件状态。不过,我还没有找到一种方法来做到这一点 这在不改变整个控件、页面或站点的状态过程的情况下是可能的吗?这似乎是一个有趣的案例,因此我尝试了一个解决方案来实现此功能。我已经想出了下面的代码,这应该是一个好的开始。它只适用于控件的“DefaultProperty”,但通过添加对特定控件类型的检查,您可以缓存和设置任何喜欢的属性 此外,它仅在第一次

我正在寻找一种简单且可扩展的方法来为用户缓存过滤器设置。我最初的想法是覆盖包含过滤器控件的面板的保存/加载ViewState进程,并在那里保存/加载控件状态。不过,我还没有找到一种方法来做到这一点


这在不改变整个控件、页面或站点的状态过程的情况下是可能的吗?

这似乎是一个有趣的案例,因此我尝试了一个解决方案来实现此功能。我已经想出了下面的代码,这应该是一个好的开始。它只适用于控件的“DefaultProperty”,但通过添加对特定控件类型的检查,您可以缓存和设置任何喜欢的属性

此外,它仅在第一次加载页面时使用缓存值设置控件。不确定是否希望在回发之间更改控件的值,以反映其他用户/会话是否进行了更改

using System.Collections.Generic;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public class CachedStatePanel : Panel
{
    private static Dictionary<string, object> CachedFilters
    {
        get
        {
            if (HttpContext.Current.Cache["CachedFilters"] == null)
            {
                HttpContext.Current.Cache["CachedFilters"] = new Dictionary<string, object>();
            }
            return (Dictionary<string, object>)HttpContext.Current.Cache["CachedFilters"];
        }
    }

    protected override void CreateChildControls()
    {
        base.CreateChildControls();

        if (!Page.IsPostBack)
        {
            foreach (Control child in Controls)
            {
                SetFromCacheRecursive(child);
            }
        }
    }

    protected override object SaveViewState()
    {
        var state = base.SaveViewState();

        if (Page.IsPostBack)
        {
            foreach (Control child in Controls)
            {
                LoadCachedStateRecursive(child);
            }
        }

        return state;
    }

    private static void LoadCachedStateRecursive(Control current)
    {
        if ((current == null) || (current.ID == null)) return;

        var attribs = current.GetType().GetCustomAttributes(typeof(DefaultPropertyAttribute), true);
        if ((attribs != null) && (attribs.Length > 0))
        {
            var propName = ((DefaultPropertyAttribute)attribs[0]).Name;
            var prop = current.GetType().GetProperty(propName);

            if (prop != null)
            {
                CachedFilters[current.ID] = prop.GetValue(current, null);
            }
        }

        if (current.HasControls())
        {
            foreach (Control child in current.Controls)
            {
                LoadCachedStateRecursive(child);
            }
        }
    }

    private static void SetFromCacheRecursive(Control current)
    {
        if ((current == null) || (current.ID == null) || (!CachedFilters.ContainsKey(current.ID))) return;

        var attribs = current.GetType().GetCustomAttributes(typeof(DefaultPropertyAttribute), true);
        if ((attribs != null) && (attribs.Length > 0))
        {
            var propName = ((DefaultPropertyAttribute)attribs[0]).Name;
            var prop = current.GetType().GetProperty(propName);

            if (prop != null)
            {
                prop.SetValue(current, CachedFilters[current.ID], null);
            }
        }

        if (current.HasControls())
        {
            foreach (Control child in current.Controls)
            {
                SetFromCacheRecursive(child);
            }
        }
    }
}
使用System.Collections.Generic;
使用系统组件模型;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
公共类CachedStatePanel:面板
{
专用静态字典缓存筛选器
{
得到
{
if(HttpContext.Current.Cache[“CachedFilters”]==null)
{
HttpContext.Current.Cache[“CachedFilters”]=new Dictionary();
}
返回(字典)HttpContext.Current.Cache[“CachedFilters”];
}
}
受保护的覆盖无效CreateChildControls()
{
base.CreateChildControls();
如果(!Page.IsPostBack)
{
foreach(控件中的控件子级)
{
SetFromCacheRecursive(子级);
}
}
}
受保护的覆盖对象SaveViewState()
{
var state=base.SaveViewState();
如果(第IsPostBack页)
{
foreach(控件中的控件子级)
{
LoadCachedStateCursive(子级);
}
}
返回状态;
}
专用静态void loadCachedStateCursive(控制当前)
{
if((current==null)| |(current.ID==null))返回;
var attribs=current.GetType().GetCustomAttributes(typeof(DefaultPropertyAttribute),true);
如果((attribs!=null)&&(attribs.Length>0))
{
var propName=((DefaultPropertyAttribute)attribs[0]).Name;
var prop=current.GetType().GetProperty(propName);
如果(prop!=null)
{
CachedFilters[current.ID]=prop.GetValue(current,null);
}
}
if(current.HasControls())
{
foreach(当前控件中的控件子级)
{
LoadCachedStateCursive(子级);
}
}
}
私有静态void SetFromCacheRecursive(控制当前)
{
if((current==null)| |(current.ID==null)| |(!CachedFilters.ContainsKey(current.ID)))返回;
var attribs=current.GetType().GetCustomAttributes(typeof(DefaultPropertyAttribute),true);
如果((attribs!=null)&&(attribs.Length>0))
{
var propName=((DefaultPropertyAttribute)attribs[0]).Name;
var prop=current.GetType().GetProperty(propName);
如果(prop!=null)
{
属性设置值(当前,CachedFilters[current.ID],null);
}
}
if(current.HasControls())
{
foreach(当前控件中的控件子级)
{
SetFromCacheRecursive(子级);
}
}
}
}

您需要保存多少信息?您可以使用cookies。我需要保存~10个控件的值。这并没有多大的节省,但我想避免手动添加未来添加到这个面板。