gpt4 book ai didi

asp.net-ajax - ScriptManager.RegisterClientScriptInclude 在 UpdatePanel 中不起作用

转载 作者:行者123 更新时间:2023-12-04 07:14:10 24 4
gpt4 key购买 nike

我已经通读了网络,但还没有找到解决以下问题的方法。我有一个带有 ScriptManager 的示例页面 (_ScriptManager.aspx) , UpdatePanel , MultiView带两个 Views和两个 LinkButtons两个 View 之间切换。第二个 View 包含一些我想为 (_ScriptManager.js) 加载 JavaScript 文件的功能。

因为我不知道用户是否会访问 View 2,所以我不想为每个请求静态地包含 javascript 文件。我只想在需要时加载它。所以我需要在异步回发期间包含脚本文件,这是我使用的 ScriptManager.RegisterClientScriptInclude为了。痛苦是:它不起作用。脚本包含以某种方式未在客户端上执行,因此无法使用其中的 javascript 函数。更糟糕的是,我在 ScriptManager.RegisterStartupScript 中注册的脚本块在这种情况下不会被执行!这一切都非常令人恼火。有趣的是,包含脚本和脚本块确实通过异步回发发送到客户端(Fiddler 是我的 friend :-))并且脚本文件也被加载。但随后 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 小时的问题调查 session ,尝试了 ScriptManager 的所有可能性,尝试使用 jQuery 代替,最后写了这个示例(因为现实世界的设置总是更复杂),我现已找到 this short blog post这解决了我的问题 - 在包含的 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();

好吧,也许这对其他人有用...

关于asp.net-ajax - ScriptManager.RegisterClientScriptInclude 在 UpdatePanel 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1916618/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com