Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 缓存控件集合时不触发自定义控件事件?_C#_Asp.net_Events_Caching_Custom Controls - Fatal编程技术网

C# 缓存控件集合时不触发自定义控件事件?

C# 缓存控件集合时不触发自定义控件事件?,c#,asp.net,events,caching,custom-controls,C#,Asp.net,Events,Caching,Custom Controls,我有一个自定义控件BedGrid,它包含一组自定义控件,每个控件上都有一个单击处理程序。在父页面的Page_Load事件中,我生成一个BedGrids集合,并将它们连接到事件处理程序。这一切都很好,当我在每一页上生成床网格时。。。单击孙子,将事件激发到BedGrid,它会提醒我的父页面,一切都按计划进行 问题是,速度太慢了。。在每个页面加载上生成所有这些自定义控件是没有意义的,尤其是在后端的访问中。因此,我想缓存床格线集合,如下所示: protected void Page_Load(objec

我有一个自定义控件BedGrid,它包含一组自定义控件,每个控件上都有一个单击处理程序。在父页面的Page_Load事件中,我生成一个BedGrids集合,并将它们连接到事件处理程序。这一切都很好,当我在每一页上生成床网格时。。。单击孙子,将事件激发到BedGrid,它会提醒我的父页面,一切都按计划进行

问题是,速度太慢了。。在每个页面加载上生成所有这些自定义控件是没有意义的,尤其是在后端的访问中。因此,我想缓存床格线集合,如下所示:

protected void Page_Load(object sender, EventArgs e)
{
    DrawBedGrids();
}

protected void DrawBedGrids()
{   
    if (CachedBedgrids == null)
    {
        CachedBedgrids = new List<BedGrid>();

        //Hit DB here and generate list of buildings....

        foreach (Building b in buildings)
        {
            BedGrid bg = new BedGrid(b);
            bg.RaiseAlertParentPage += new EventHandler(BedGrid_Clicked);
            CachedBedgrids.Add(bg);
        }
    }
    else
    {
        foreach (BedGrid bg in CachedBedgrids)
        {
            somePanel.Controls.Add(bg);
        }
    }
}

protected List<BedGrid> CachedBedgrids
{
    get
    {
        try { return (List<BedGrid>)Session["CachedBedgrids"]; }
        catch { return null; }
    }
    set { Session["CachedBedgrids"] = value; }
}
在将床格线添加到面板之前,单击“else”


我错过了什么?所有这些都发生在页面加载中,那么为什么事件没有触发呢?其他一切正常,这意味着控件及其子控件绘制正确。

这不起作用的原因是,控件必须在回发帖上重新创建,事件必须连接,才能触发

请参阅,因为服务器是无状态的,所以要在对象上触发事件,需要重新创建该对象并将其读入表单


不如缓存数据库结果,然后继续循环,否则将构建新控件、添加控件并连接控件?

谢谢,我来试一试。自从提出这个问题以来,我一直被其他形式的问题所困扰。@Glendale,别担心。总之,ASP.NET事件模型在动态创建控件时充其量是痛苦的。。我在自定义控件集合中嵌套了自定义控件集合,所有这些控件都是动态创建的。。说把我的头发拔出来!我真的希望可以缓存控件本身,因为它们在会话过程中不会有太大变化。我想我只能接受这些数据了。
bg.RaiseAlertParentPage += new EventHandler(BedGrid_Clicked);