ScriptManager.RegisterClientScriptInclude 在 UpdatePanel 中不起作用
我已經通讀了網路,但沒有找到解決以下問題的方法。我有這個範例頁面(_ScriptManager.aspx),其中一個
ScriptManager、一個UpdatePanel、一個MultiView和兩個Views和兩個LinkButtons兩個在視圖之間切換。第二個視圖包含一些我想為 (_ScriptManager.js) 載入 JavaScript 文件的功能。由於我不知道使用者是否會訪問視圖 2,我不想在每個請求中靜態包含 javascript 文件。我只想在需要時載入它。所以我需要在我使用的非同步回發期間包含腳本文件
ScriptManager.RegisterClientScriptInclude。痛苦是:它不起作用。腳本包含在客戶端上以某種方式未執行,因此無法使用其中的 javascript 函式。更糟糕的是,在這種情況下,我註冊的腳本塊ScriptManager.RegisterStartupScript不會被執行!這一切都非常令人惱火。有趣的是,包含腳本和腳本塊確實通過非同步回發發送到客戶端(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 的 span 中):
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); } } }
好吧,這太令人震驚了:在對問題進行了兩個 2 小時的調查後,嘗試了 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();好吧,也許這對那裡的其他任何人都有用…