母版页中的异步触发 UpdatePanel 在连续循环中插入 js 脚本
Async triggered UpdatePanel on masterpage inserting js scripts on continuous loop
我的母版页上有一个更新面板,它异步发布到服务器以便将一些数据插入到标签中
<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 插入不断循环插入,从而导致疯狂的不稳定问题。
如果我能得到一个 reason/answer 为什么 js 插入在母版页计时器事件的每个 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 上的这些常量插入,或者计时器系统是否强制要求这些?
在发出脚本的代码周围放置一个 if,您说的是在每个计时器 postback 上插入到头部部分。如果计时器触发其 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 或 non-ajax post 返回时更新,post 的来源无关紧要返回起源。
因此,在您的情况下,计时器控件正在 post 返回,但是除了包含标签的更新面板之外,带有树视图的更新面板也会更新,因为树视图更新面板设置为始终由于 UpdateMode='Always'
.
的默认值而自行更新
我的母版页上有一个更新面板,它异步发布到服务器以便将一些数据插入到标签中
<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 插入不断循环插入,从而导致疯狂的不稳定问题。 如果我能得到一个 reason/answer 为什么 js 插入在母版页计时器事件的每个 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 上的这些常量插入,或者计时器系统是否强制要求这些?
在发出脚本的代码周围放置一个 if,您说的是在每个计时器 postback 上插入到头部部分。如果计时器触发其 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 或 non-ajax post 返回时更新,post 的来源无关紧要返回起源。
因此,在您的情况下,计时器控件正在 post 返回,但是除了包含标签的更新面板之外,带有树视图的更新面板也会更新,因为树视图更新面板设置为始终由于 UpdateMode='Always'
.