I have a custom control "BedGrid" that contains a collection of custom controls, each of which has a click handler on them. In the Page_Load event of my Parent page, I generate a collection of BedGrids, and wire them up to an event handler. This all works fine, when I generate the BedGrids on each Page_Load... The grandchild is clicked, fires the event up to the BedGrid, which alerts my Parent Page and everything goes as planned.
The problem is, it's slow.. Generating all those custom controls on each Page_Load doesn't make sense (especially with trips to the backend). So, I want to cache the collection of BedGrids like so:
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; }
}
And it all breaks.. The events never fire... Even if I add
bg.RaiseAlertParentPage += new EventHandler(BedGrid_Clicked);
to the "else" right before I add the BedGrid to the panel..
What am I missing? All of this is happening in Page_Load, so why is the event not firing? Everything else is fine, meaning that the controls and their children draw properly..