Search code examples
javascriptjqueryasp.netasynchronousupdatepanel

Async triggered UpdatePanel on masterpage inserting js scripts on continuous loop


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?


Solution

  • 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'.