I have controls being dynamically added and recreated on a web dashboard. I have been attempting to reduce my usage of Page.FindControl; I found it to be slow with large amounts of dynamically created content on Page.
In my attempts, I created a RegenerationManager singleton which is now in charge of regenerating my dynamic content. Whenever it regenerates an object, it stores the object in a list. This manager keeps several of these lists based on type of object.
Later, when I need to fetch an object by ID, I go to my regeneration manager to retrieve the object.
E.G:
public class RegenerationManager
{
private static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private static readonly RegenerationManager instance = new RegenerationManager();
private RegenerationManager() { }
public static RegenerationManager Instance
{
get { return instance; }
}
public List<CormantRadPane> RegeneratedPanes = new List<CormantRadPane>();
public List<CormantRadDockZone> RegeneratedDockZones = new List<CormantRadDockZone>();
/// <summary>
/// Recreates all the dynamically made DockZones.
/// </summary>
public void RegenerateDockZones()
{
Logger.Info("Regenerating dock zones.");
foreach (KeyValuePair<string, RadDockZoneSetting> dockZoneState in RadControlManager.GetStates<SerializableDictionary<string, RadDockZoneSetting>>())
{
try
{
RadDockZoneSetting dockZoneSetting = dockZoneState.Value as RadDockZoneSetting;
Logger.Info(String.Format("Loading state data for dock zone with setting ID: {0}", dockZoneSetting.ID));
CormantRadDockZone dockZone = new CormantRadDockZone(dockZoneSetting);
RegeneratedDockZones.Add(dockZone);
CormantRadPane pane = RegeneratedPanes.First(regeneratedPane => regeneratedPane.ID == dockZoneSetting.ParentID);
pane.Controls.Add(dockZone);
}
catch (Exception exception)
{
Logger.ErrorFormat("Error regenerating dock zones. Reason: {0}", exception.Message);
}
}
}
}
/// <summary>
/// Creates the RadDock + Contents when dropping a graph onto the page.
/// </summary>
/// <param name="sender"> The RadListBox with an element being dropped from inside it. </param>
/// <param name="e"> Information about the drop such as target and what is being dropped. </param>
protected void RadListBox_Dropped(object sender, RadListBoxDroppedEventArgs e)
{
CormantRadDockZone dockZoneOld = Utilities.FindControlRecursive(Page, e.HtmlElementID) as CormantRadDockZone;
CormantRadDockZone dockZone = RegenerationManager.Instance.RegeneratedDockZones.First(regeneratedDockZone => regeneratedDockZone.ID == e.HtmlElementID);
if (!object.Equals(dockZone, null) && !dockZone.Docks.Any())
{
RadSlidingPane slidingPane = ((sender as RadListBox).Parent as RadSlidingPane);
CormantReport report = new CormantReport(int.Parse(e.SourceDragItems[0].Value), e.SourceDragItems[0].Text, slidingPane.Title);
CormantRadDock dock = new CormantRadDock(report);
dock.CreateContent();
dockZone.Controls.Add(dock);
}
}
Now, I thought this was a pretty good solution, but upon testing I found that the CormantRadDock being added to dockZone does not show up on my web dashboard, but if I added it to dockZoneOld that it does.
Looking at the two objects I did not notice any properties different about the two, but obviously there is something.
Is my idea possible to implement?
EDIT: I suspect the problem is with Telerik’s controls. I have an idea I’ll report back in a bit.
EDIT: It was indeed a Telerik thing. I needed to go through their RadDockLayout.RegisteredDockZone’s list instead of keeping my own managed one.
The key thing to do here is debug your code and put a breakpoint on this line:
Then do a quickwatch on the contents of
RegenerationManager.Instance.RegeneratedDockZonesto see if that list has any elements at in it. So here we’re checking the basics – does the list have any items in it at all?If there are items in there, you need to figure out why the specific element your looking for is not in the list. The list must be working, as there are items in there, but this specific element has not been added.
If you’re 100% that the element is being added to the list, then it could be something to do with the singleton pattern your using. To be honest, it looks OK to me, but I’m no expert on the singleton design pattern.
Sorry I can’t be of more help.
EDIT:
One thing which might help is to refactor the class a little bit:
Here I’m using properties to return the lists, rather then giving people direct access to the list members.
This could help you with debugging your code.