ネットを読んだのですが、次の問題の解決策が見つかりませんでした。このサンプルページ(_ScriptManager.aspx)には、ScriptManagerビューを切り替える2つと2つの2つのスイッチがあります。2番目のビューには、(_ ScriptManager.js)のJavaScriptファイルをロードしたいいくつかの機能が含まれています。UpdatePanelMultiViewViewsLinkButtons
ユーザーがビュー2にアクセスするかどうかわからないため、リクエストごとにJavaScriptファイルを静的にオンにしたくありません。必要なときにだけロードしたい。そのため、非同期ポストバック中にスクリプトファイルを含める必要があります。これは私が使用するScriptManager.RegisterClientScriptInclude目的です。痛みは:それは機能しません。スクリプトインクルードはクライアントで実行されないため、内部のjavascript関数を使用できません。さらに悪いことに、ScriptManager.RegisterStartupScriptこの場合、登録したスクリプトブロックは実行されません。これはすべて非常に苛立たしいことです。興味深いのは、includeスクリプトとscriptブロックが非同期ポストバック(Fiddlerは私の友達です:-))でクライアントに送信され、スクリプトファイルも読み込まれることです。しかし、JavaScriptはどういうわけか壊れているようです...
誰かが手がかりを持っているか、報告されたバグを知っていますか?
_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);
}
}
}