异步触发了在连续循环上插入js脚本的母版页上的UpdatePanel

时间:2016-01-12 16:55:46

标签: javascript jquery asp.net asynchronous updatepanel

我的母版页上有一个更新面板,异步发布到服务器以便将一些数据插入到标签中

<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>

内容页面回发导致asp.net的js插入树视图和更新面板不断插入循环导致疯狂不稳定问题。 如果我能得到一个理由/答案为什么js插入继续插入来自masterpages计时器事件的每个onTick

我对回发有好处

更新

标签应该每隔几秒更新一次, 并且有问题的树视图位于内容页面上,其中计时器和更新面板位于主服务器上。

tick事件的代码如下

_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; }

更新

每次计时器滴答时,此代码都会重新插入到页面的HEAD中,但原始插入仍然存在,因此它只是不断添加,并添加直到页面滞后

<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>

更新

所以将它们全部设置为条件确实会停止连续的树视图插入,谢谢。唯一的问题是,每个刻度线下面的插入物现在都是连续的。

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

我可以在每个timerTick上删除这些常量插入,还是计时器系统强制要求这些插入?

1 个答案:

答案 0 :(得分:0)

在发出脚本的代码周围放一个if,你要说的是在每个定时器回发中插入到head部分。如果计时器触发其Tick事件,则此if不会插入脚本。

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

此外,请确保母版页中的所有更新面板以及其中UpdateMode='Conditional'的所有更新面板。

此设置的默认值为“始终”,即更新每个ajax或非ajax帖子的面板更新,这与发回的位置无关。

因此,在您的情况下,计时器控件正在回发,但是除了包含标签的更新面板之外,具有树视图的更新面板也会更新,因为树视图更新面板设置为始终由于默认值而更新自身UpdateMode='Always'