ScriptManager.RegisterClientScriptInclude不工作的UpdatePanel
-
20-09-2019 - |
题
我已经通过网络阅读,但还没有找到一个解决以下问题。我有这个例子页(_ScriptManager.aspx)与ScriptManager
,一个UpdatePanel
,具有两个MultiView
和视图之间的两个Views
2开关LinkButtons
。第二视图包含一些功能,其欲加载(_ScriptManager.js)JavaScript文件。
由于我不知道用户是否能访问视图我不想包含的JavaScript文件静态地对每一个要求。我只是想,如果当我需要它来加载它。所以,我需要包括异步回发这是我用ScriptManager.RegisterClientScriptInclude
用于在脚本文件。疼痛:这是行不通的。该脚本包括以某种方式不是在客户端上让里面的JavaScript功能不能使用执行。更糟的是,脚本块我在ScriptManager.RegisterStartupScript
注册没有得到在这种情况下执行!这是非常恼人。有趣的是,包括脚本,脚本块就被发送到与异步回发的客户端(提琴手是我的朋友:-)),并且还加载脚本文件。但随后的JavaScript以某种方式似乎打破...
有没有人有一个线索或知道报告的bug吗?
<强> _ScriptManager.aspx 强>:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="_ScriptManager.aspx.cs" Inherits="Frontend.Web._Tests.ScriptManagerTest" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.3.2.min.js"></script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="scm"></asp:ScriptManager>
<asp:Label runat="server" ID="lbOut">Outside of UpdatePanel</asp:Label>
<div style="border: solid 1px red;">
<asp:UpdatePanel runat="server" ID="up" UpdateMode="Conditional">
<ContentTemplate>
<div>
<asp:LinkButton runat="server" ID="btnFirst">Show view 1</asp:LinkButton>
<asp:LinkButton runat="server" ID="btnSecond">Show view 2</asp:LinkButton>
</div>
<div>
<asp:MultiView runat="server" ID="mv">
<asp:View runat="server" ID="vw1">First view - static content</asp:View>
<asp:View runat="server" ID="vw2">
Second view - dynamically loaded content (between dashes):
<div>#<span id="divDyn"></span>#</div>
</asp:View>
</asp:MultiView>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
<强> _ScriptManager.js 强>(这里我只是添加一些动态内容使用id = divDyn跨度):
function dynamic() {
alert('dynamic');
$('#divDyn').text('Dynamic!');
}
<强> _ScriptManager.aspx.cs 强>代码隐藏:
public partial class ScriptManagerTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
btnFirst.Click += delegate { mv.SetActiveView(vw1); };
btnSecond.Click += delegate { mv.SetActiveView(vw2); };
if (!IsPostBack)
{
// Test 1: does not work
mv.SetActiveView(vw1);
// Test 2: works, because required script is loaded on initial page request
//mv.SetActiveView(vw2);
}
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (mv.GetActiveView() == vw2)
{
// Not calling the RegisterClientScriptInclude
// makes the alert work in both cases, but this is
// what it's all about: including script only when
// needed!
ScriptManager.RegisterClientScriptInclude(
Page, Page.GetType(), "include-js",
ResolveClientUrl("~/ScriptManager.js"));
ScriptManager.RegisterStartupScript(
this, GetType(), "call-dynamic",
"alert('hi there'); dynamic();", true);
}
}
}
解决方案
那么,这是惊天动地的:后调查的问题,OUF尝试所有的的ScriptManager的可能性两个小时的会议,试图使用jQuery,而不是最后写了这个样品(因为现实世界中的设置是一如既往更复杂),我现在发现这短短的博客帖子它解决了我的问题 - 添加了一行JS文件包括:
<强> _ScriptManager.js 强>:
function dynamic() {
alert('dynamic');
$('#divDyn').text('Dynamic!');
}
// notify that the script has been loaded <-- new!
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
好吧,也许这将是任何使用其他任何人在那里的......