I've created a custom template control that I'm trying to use to delay content loading on a page for long running controls.
Here:
1 public sealed class DelayedContentLoader : WebControl, INamingContainer
2 {
3 private MultiView mvContent = new MultiView();
4
5 private int interval = 10000;
6 public int Interval
7 {
8 get { return interval; }
9 set { interval = value; }
10 }
11
12 public delegate bool LoadContentHandler();
13 public LoadContentHandler OnLoadContent;
14
15 private ITemplate loadingTemplate;
16 [TemplateContainer(typeof(LoadingView))]
17 public ITemplate LoadingTemplate
18 {
19 get { return loadingTemplate; }
20 set { loadingTemplate = value; }
21 }
22
23 private ITemplate loadFailedTemplate;
24 [TemplateContainer(typeof(LoadFailedView))]
25 public ITemplate LoadFailedTemplate
26 {
27 get { return loadFailedTemplate; }
28 set { loadFailedTemplate = value; }
29 }
30
31 private ITemplate contentTemplate;
32 [TemplateContainer(typeof(ContentView))]
33 public ITemplate ContentTemplate
34 {
35 get { return contentTemplate; }
36 set { contentTemplate = value; }
37 }
38
39 protected override void OnInit(EventArgs e)
40 {
41 if (loadingTemplate != null)
42 {
43 LoadingView loadingViewContainer = new LoadingView();
44 loadingTemplate.InstantiateIn(loadingViewContainer);
45 mvContent.Views.Add(loadingViewContainer);
46 }
47
48 if (contentTemplate != null)
49 {
50 ContentView contentViewContainer = new ContentView();
51 contentTemplate.InstantiateIn(contentViewContainer);
52 mvContent.Views.Add(contentViewContainer);
53 }
54 else
55 throw new Exception("A Content Template must be specified.");
56
57 if (loadFailedTemplate != null)
58 {
59 LoadFailedView loadFailedViewContainer = new LoadFailedView();
60 loadFailedTemplate.InstantiateIn(loadFailedViewContainer);
61 mvContent.Views.Add(loadFailedViewContainer);
62 }
63 }
64
65 protected override void OnLoad(EventArgs e)
66 {
67 UpdatePanel updatePanel = new UpdatePanel();
68 updatePanel.UpdateMode = UpdatePanelUpdateMode.Always;
69
70 mvContent.ActiveViewIndex = 0;
71
72 updatePanel.ContentTemplateContainer.Controls.Add(mvContent);
73
74 Timer timer = new Timer();
75 timer.ID = "timerDelayedContentLoader";
76 timer.Interval = interval;
77 timer.Tick += new EventHandler(timer_Tick);
78
79 updatePanel.ContentTemplateContainer.Controls.Add(timer);
80
81 Controls.Add(updatePanel);
82 }
83
84 public override Control FindControl(string id)
85 {
86 Control ctrl = mvContent.Views[1].FindControl(id);
87 if (ctrl != null)
88 return ctrl;
89
90 ctrl = mvContent.Views[2].FindControl(id);
91 if (ctrl != null)
92 return ctrl;
93
94 ctrl = mvContent.Views[0].FindControl(id);
95
96 return ctrl;
97 }
98
99 private void timer_Tick(object sender, EventArgs e)
100 {
101 bool loadedContentSuccessfully = false;
102 if (OnLoadContent != null)
103 loadedContentSuccessfully = OnLoadContent();
104
105 // Stop the timer.
106 Timer timer = FindControl("timerDelayedContentLoader") as Timer;
107 if (timer != null)
108 timer.Enabled = false;
109
110 int visiblePlh = loadedContentSuccessfully ? 1 : 2;
111 mvContent.ActiveViewIndex = visiblePlh;
112 }
113 }
The control does work, but once the first 'panel' loads I'm getting this error:
Error: Sys.ScriptLoadFailedException: The script 'http://localhost:2059/WebResource.axd?d=2969z1Qx3dbsV5nCF1BRcXhpF5ub86-cDZ-FaGYdZEBQbKzZVJb7tl6DB7CRlXIMyp8MDajf0oi_5PQ_Zb35Pg2&t=633181289164114804' failed to load. Check for:
Inaccessible path.
Script errors. (IE) Enable 'Display a notification about every script error' under advanced settings.
Missing call to Sys.Application.notifyScriptLoaded().
Source File: http://localhost:2059/ScriptResource.axd?d=w7roB_KjU8DhSirRDxfFxPRhP-DUqImf_4Nd1a5Co3kx1o511QAFqGXxMtkfZR8UkArHbaLDYRdK8lHjJK-ARWQArhEXy5Ydco_GHuMUgH41&t=633120912402558276
Line: 3311
The error kills all of the JavaScript on the page - others delayed content loaders stop loading and all other AJAX stuff ceases to work. The line that is referenced is this line #10 below:
// MicrosoftAjax.js
// Microsoft AJAX Framework.
1 function Sys$_ScriptLoader$_raiseError(multipleCallbacks) {
2 var callback = this._scriptLoadFailedCallback;
3 var scriptElement = this._currentTask.get_scriptElement();
4 this._stopLoading();
5
6 if(callback) {
7 callback(this, scriptElement, multipleCallbacks);
8 }
9 else {
10 throw Sys._ScriptLoader._errorScriptLoadFailed(scriptElement.src, multipleCallbacks);
11 }
12 }
Is there anything that can be done here? By the way, this control is inside of a Web Part. If I remove all the Web Part stuff, I don't get the error but only one of my delayed content loading controls finishes loading.
Any help would be much appreciated.
Thanks!