0

I have an update panel on my master page that async posts to the server in order to get some data inserted into a label

<asp:Timer ID="timer1" runat="server" Interval="1000" OnTick="timer1_Tick"></asp:Timer>
<asp:UpdatePanel ID="upTimer1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
<asp:Label ID="lab" runat="server"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>

The content page postback is causing the asp.net's js inserts for treeviews and updatepanels to continuously insert on loop causing mad instability issues. I'm okay with the postbacks if i can get a reason/answer to why the js inserts keep inserting with every onTick from the masterpages timer event

UPDATE

the label should update every few seconds, and the treeview in question is on the content page where as the timer and update panel are on the master.

The code on the tick event is as follows

_sql = new SQL(ConfigurationManager.ConnectionStrings["connString"].ConnectionString);
DataTable dt = _sql.GetMessages(Session["User"].ToString()).Tables[0];
int count = 0;
if (dt.Rows.Count != 0) { for (int i = 0; i < dt.Rows.Count; i++) { if (!(bool)dt.Rows[i]["messageRead"]) { count++; } } }
if (count > 0)
{
if (count > 99) { lab.Text = "99+"; }
else { lab.Text = count.ToString(); }
}
else { lab.Text = string.Empty; }

UPDATE

Everytime the timer ticks this code gets reinserted into the HEAD of the page but the original insert is still there so it just keeps adding, and adding until the page lags down

<script type="text/javascript">var cphHeropath_tvEvents_Data = null;</script>
<script type="text/javascript">
Sys.WebForms.PageRequestManager._addArrayElement('cphHeropath_tvEvents_ImageArray', '');
Sys.WebForms.PageRequestManager._addArrayElement('cphHeropath_tvEvents_ImageArray', '');
Sys.WebForms.PageRequestManager._addArrayElement('cphHeropath_tvEvents_ImageArray', '');
Sys.WebForms.PageRequestManager._addArrayElement('cphHeropath_tvEvents_ImageArray', '/WebResource.axd?d=Vnw6Nul5Gi8dLBSg8ZsULZm-q_TThJrtFUwhdaYiJR2-SI14Y5nZERvMBuf1pp3hANJ4ymdDltekmivTd-IP0DmYagXAChzkcq2C4A4ZwOw1&t=635858559402388336');
Sys.WebForms.PageRequestManager._addArrayElement('cphHeropath_tvEvents_ImageArray', '/WebResource.axd?d=Wt-oJLHV-dsfkxJN06L-WS7YzkkDgeALhClv1gm-ITLCsWITJpTR205JjsXo0RKET50tKis10XGsSGwW5yACH_IdZeEk-V6y3LfMIUUOAX41&t=635858559402388336');
Sys.WebForms.PageRequestManager._addArrayElement('cphHeropath_tvEvents_ImageArray', '/WebResource.axd?d=-n2KnhPEOy8tY2UhU96Dm6tpQMQ-k0JQe8bafZ_4g-kOfLnmWNaKU3HwLJdqrAZWpk0NCRVSeF9MRKP0SNO90D6BbAstCcDuQ1xlzxqB4co1&t=635858559402388336');
Sys.WebForms.PageRequestManager._addArrayElement('cphHeropath_tvEventListMovement_Teleport_ImageArray', '');
Sys.WebForms.PageRequestManager._addArrayElement('cphHeropath_tvEventListMovement_Teleport_ImageArray', '');
Sys.WebForms.PageRequestManager._addArrayElement('cphHeropath_tvEventListMovement_Teleport_ImageArray', '');
Sys.WebForms.PageRequestManager._addArrayElement('cphHeropath_tvEventListMovement_Teleport_ImageArray', '/WebResource.axd?d=Vnw6Nul5Gi8dLBSg8ZsULZm-q_TThJrtFUwhdaYiJR2-SI14Y5nZERvMBuf1pp3hANJ4ymdDltekmivTd-IP0DmYagXAChzkcq2C4A4ZwOw1&t=635858559402388336');
Sys.WebForms.PageRequestManager._addArrayElement('cphHeropath_tvEventListMovement_Teleport_ImageArray', '/WebResource.axd?d=Wt-oJLHV-dsfkxJN06L-WS7YzkkDgeALhClv1gm-ITLCsWITJpTR205JjsXo0RKET50tKis10XGsSGwW5yACH_IdZeEk-V6y3LfMIUUOAX41&t=635858559402388336');
Sys.WebForms.PageRequestManager._addArrayElement('cphHeropath_tvEventListMovement_Teleport_ImageArray', '/WebResource.axd?d=-n2KnhPEOy8tY2UhU96Dm6tpQMQ-k0JQe8bafZ_4g-kOfLnmWNaKU3HwLJdqrAZWpk0NCRVSeF9MRKP0SNO90D6BbAstCcDuQ1xlzxqB4co1&t=635858559402388336');
Sys.WebForms.PageRequestManager._addArrayElement('cphHeropath_tvDatabaseAssets_ImageArray', '');
Sys.WebForms.PageRequestManager._addArrayElement('cphHeropath_tvDatabaseAssets_ImageArray', '');
Sys.WebForms.PageRequestManager._addArrayElement('cphHeropath_tvDatabaseAssets_ImageArray', '');
Sys.WebForms.PageRequestManager._addArrayElement('cphHeropath_tvDatabaseAssets_ImageArray', '/WebResource.axd?d=Vnw6Nul5Gi8dLBSg8ZsULZm-q_TThJrtFUwhdaYiJR2-SI14Y5nZERvMBuf1pp3hANJ4ymdDltekmivTd-IP0DmYagXAChzkcq2C4A4ZwOw1&t=635858559402388336');
Sys.WebForms.PageRequestManager._addArrayElement('cphHeropath_tvDatabaseAssets_ImageArray', '/WebResource.axd?d=Wt-oJLHV-dsfkxJN06L-WS7YzkkDgeALhClv1gm-ITLCsWITJpTR205JjsXo0RKET50tKis10XGsSGwW5yACH_IdZeEk-V6y3LfMIUUOAX41&t=635858559402388336');
Sys.WebForms.PageRequestManager._addArrayElement('cphHeropath_tvDatabaseAssets_ImageArray', '/WebResource.axd?d=-n2KnhPEOy8tY2UhU96Dm6tpQMQ-k0JQe8bafZ_4g-kOfLnmWNaKU3HwLJdqrAZWpk0NCRVSeF9MRKP0SNO90D6BbAstCcDuQ1xlzxqB4co1&t=635858559402388336');
Sys.WebForms.PageRequestManager._addArrayElement('cphHeropath_tvGameAssets_ImageArray', '');
Sys.WebForms.PageRequestManager._addArrayElement('cphHeropath_tvGameAssets_ImageArray', '');
Sys.WebForms.PageRequestManager._addArrayElement('cphHeropath_tvGameAssets_ImageArray', '');
Sys.WebForms.PageRequestManager._addArrayElement('cphHeropath_tvGameAssets_ImageArray', '/WebResource.axd?d=Vnw6Nul5Gi8dLBSg8ZsULZm-q_TThJrtFUwhdaYiJR2-SI14Y5nZERvMBuf1pp3hANJ4ymdDltekmivTd-IP0DmYagXAChzkcq2C4A4ZwOw1&t=635858559402388336');
Sys.WebForms.PageRequestManager._addArrayElement('cphHeropath_tvGameAssets_ImageArray', '/WebResource.axd?d=Wt-oJLHV-dsfkxJN06L-WS7YzkkDgeALhClv1gm-ITLCsWITJpTR205JjsXo0RKET50tKis10XGsSGwW5yACH_IdZeEk-V6y3LfMIUUOAX41&t=635858559402388336');
Sys.WebForms.PageRequestManager._addArrayElement('cphHeropath_tvGameAssets_ImageArray', '/WebResource.axd?d=-n2KnhPEOy8tY2UhU96Dm6tpQMQ-k0JQe8bafZ_4g-kOfLnmWNaKU3HwLJdqrAZWpk0NCRVSeF9MRKP0SNO90D6BbAstCcDuQ1xlzxqB4co1&t=635858559402388336');
</script>

UPDATE

so setting them up all to conditional did stop the continuous treeview inserts so thank you. the only thing is with every tick the following inserts are continuous now.

<script>
Sys.Application.add_init(function() {
    $create(Sys.UI._Timer, {"enabled":true,"interval":1000,"uniqueID":"ctl00$timer"}, null, null, $get("timer"));
});
</script>

Is it possible for me to remove these constant inserts on each timerTick or is this mandatory by the timer system to have these?

  • Try adding UpdateMode="Conditional" to your UpdatePanel. – Kami Jan 12 '16 at 17:00
  • If you use a timer control, and hook it up with an UpdatePanel which you have done, then every time the `Tick` event occurs the UpdatePanel contents would be updated. That is how a a timer works. – Sunil Jan 12 '16 at 17:25
  • Do you just want to update the label control only once, or you want to update it every few minutes? Also, please post the code for `Tick` event as there may be some code which is updating tree view. – Sunil Jan 12 '16 at 17:26
  • @Sunil i updated the question to reflect your response – Terrance Fairbairn Jan 13 '16 at 18:18
  • Is the treeview in content page also in an UpdatePanel? – Sunil Jan 13 '16 at 18:19
  • What do you mean by `js inserts`? – Sunil Jan 13 '16 at 18:21
  • Try this: Put the Timer inside the UpdatePanel that contains the label you want to update. Right now its outside. If you want to update only a single UpdatePanel with timer, then it needs to be part of ContentTemplate of that UpdatePanel. – Sunil Jan 13 '16 at 18:27
  • @Sunil tried moving the timers into the content template but still the same issue – Terrance Fairbairn Jan 13 '16 at 18:29
  • Also, make sure that the UpdatePanel that contains the treeview has its UpdateMode="Conditional". Do you have more than one timer since you said `timers`? – Sunil Jan 13 '16 at 18:31
  • After your recent update, what you have is normal behavior. Since the whole page is not refreshing so the script keeps being added in head section. Where are these scripts coming from? – Sunil Jan 13 '16 at 18:35
  • May be you need put an if around the statement in your code-behind that emits this JavaScript. You can check whether the async postback element id is that of timer, and if yes then do not emit the JavaScript. I guess you are using `ScriptManager.RegisterStartupScript` to emit this script. – Sunil Jan 13 '16 at 18:37
  • Check the answer I just posted. – Sunil Jan 13 '16 at 18:45
  • @Sunil yes i am cuz asp.net treeviews need a script manager and i'm pretty sure these scripts get pasted into the HEAD by default for them to operate and for whatever reason when a postback occurs, even in a async triggered update panel, the script manager reinserts the code without removing or checking if it already exists. Perhaps i'll have to go about loading the whole system in another way then. Thankxs for the help – Terrance Fairbairn Jan 13 '16 at 18:46
  • These `$create` are automatically inserted by ASP.Net and they wil `NEVER` cause issues since ASP.Net is all well-tested with these scripts. – Sunil Jan 13 '16 at 20:37
  • It seems you did not read my answer. Only change required is to set the UpdateMode = 'Conditional` for all other update panels other than the label panel. Where is the problem after you do all this? – Sunil Jan 13 '16 at 20:39

1 Answers1

0

Put an if around the code that emits the script which you are saying is getting inserted into the head section on every timer postback. This if will not insert the script if the timer fires its Tick event.

if (ScriptManager.GetCurrent(this.Page).AsyncPostBackSourceElementID.IndexOf("timer1") == -1)
 {
  ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
                  "myscript", "var x=10;", true);
 }

Also, make sure that all update panels in the master page and else where have their UpdateMode='Conditional'.

The default value of this setting is Always i.e. update panel updates on every ajax or non-ajax post back and it doesn't matter from where the post back originated.

So, in your case the timer control is posting back, but the update panel with tree view is also getting updated in addition to update panel containing the label, since tree view update panel set to always update itself due to the default value of UpdateMode='Always'.

Sunil
  • 20,653
  • 28
  • 112
  • 197
  • the script is self generating from the asp.net treeviews default state and use of script manager, in other words i'm not pasting this code the treeview itself is – Terrance Fairbairn Jan 13 '16 at 18:49
  • then please paste the master page code since there is some issue there. – Sunil Jan 13 '16 at 18:52
  • i suspect that an update panel in master page is always set to update. – Sunil Jan 13 '16 at 18:58
  • so make sure that the update panels in your master page have this property set: `UpdateMode = 'Conditional'` – Sunil Jan 13 '16 at 19:02
  • Okay so your saying all my update panels should be conditional then? cuz yeah some might be set to always since always is default if im not mistaken. I'll let you know if that works asap – Terrance Fairbairn Jan 13 '16 at 19:03
  • Yes, otherwise the update panel will always update on every post back. Note that if you do not set this property then the default setting is `Always` i.e. update panel updates on every post back and it doesn't matter from where the post back happened. – Sunil Jan 13 '16 at 19:04
  • No you cannot remove the timer script as its automatically inserted by ASP.Net so the timer works as expected. Since your timer posts back every few minutes, it needs that script else it will not fire at all. THAT IS NO PROBLEMS AND WILL NEVER CAUSE ISSUES. – Sunil Jan 13 '16 at 20:25
  • Right on it seems to be working at a good stable state now so thank you very much you have been a great help. I'm new on stackOverflow so I don't have enough rep to arrow up your answer, i tried it said i needed 15, but your answer is the right one atleast in my situation. – Terrance Fairbairn Jan 13 '16 at 20:38
  • Look at this post on how to mark an answer: http://meta.stackexchange.com/questions/147531/how-mark-my-question-as-answered-on-stackoverflow. – Sunil Jan 13 '16 at 20:44
  • All you need to do is click on the grayed out checkbox next to the answer. I have flagged this for moderator attention. – Sunil Jan 13 '16 at 20:48
  • @Sunil:Can you help me with this question please because i dont have much idea regarding update panel:http://stackoverflow.com/questions/34605384/what-is-the-correct-way-to-put-multiple-controls-inside-update-panel – I Love Stackoverflow Jan 23 '16 at 06:40
  • @Learning, I just posted an answer to your above question. Let me know if it helps you. – Sunil Jan 23 '16 at 16:53