ScriptManager.RegisterClientScriptInclude不UpdatePanel的工作 [英] ScriptManager.RegisterClientScriptInclude does not work in UpdatePanel

查看:211
本文介绍了ScriptManager.RegisterClientScriptInclude不UpdatePanel的工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过网络阅读,但还没有找到一个解决以下问题。我有这个示例页面(_ScriptManager.aspx)以的ScriptManager ,一个的UpdatePanel ,一个多视图有两个查看和两个 LinkBut​​tons 的意见之间的两个交换。第二种观点包含了一些功能,我想加载(_ScriptManager.js)的JavaScript文件。

由于我不知道用户是否能访问视图2我不希望包含的JavaScript文件静态上为每个请求。我只是想,如果当我需要它加载它。所以,我需要包括异步回发在脚本文件这是我用 ScriptManager.RegisterClientScriptInclude 的。疼痛:这是行不通的。该脚本包含不以某种方式在客户端上,使里面的JavaScript函数不能用于执行。更糟的是,脚本块我 ScriptManager.RegisterStartupScript 注册没有得到在这种情况下执行!这是非常讨厌。有趣的是,包括脚本和脚本块就被发送到与异步回发客户端(提琴手是我的朋友:-)),并且还装载脚本文件。但随后的JavaScript以某种方式似乎打破...

有没有人有一个线索或知道报告的bug吗?

_ScriptManager.aspx

 <%@页面语言=C#AutoEventWireup =真codeBehind =_ ScriptManager.aspx.cs继承=Frontend.Web._Tests.ScriptManagerTest %GT;!< D​​OCTYPE HTML PUBLIC -  // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">< HTML的xmlns =htt​​p://www.w3.org/1999/xhtml>
<头=服务器>
  <标题>< /标题>
  &所述; SCRIPT SRC =htt​​p://ajax.microsoft.com/ajax/jQuery/jquery-1.3.2.min.js>&下; /脚本>
< /头>
<身体GT;
  <表ID =form1的=服务器>
    < ASP:的ScriptManager =服务器ID =SCM>< / ASP:ScriptManager的>
    < ASP:标签=服务器ID =LBOUT>的UpdatePanel外方式< / ASP:标签>
  < D​​IV的风格=边界:1px的固体红;>
      < ASP:的UpdatePanel =服务器ID =向上的UpdateMode =条件>
        <&的ContentTemplate GT;
          < D​​IV>
            < ASP:LinkBut​​ton的=服务器ID =btnFirst>显示视图1 LT; / ASP:LinkBut​​ton的>
            < ASP:LinkBut​​ton的=服务器ID =btnSecond>显示视图2'; / ASP:LinkBut​​ton的>
          < / DIV>
          < D​​IV>
            < ASP:多视图=服务器ID =MV>
              < ASP:查看=服务器ID =VW1>首先查看 - 静态内容和LT; / ASP:视图>
              < ASP:查看=服务器ID =VW2>
                第二种观点 - 动态加载的内容(破折号之间):
                < D​​IV>#<跨度ID =divDyn>< / SPAN>#< / DIV>
              < / ASP:视图>
            < / ASP:多视图>
          < / DIV>
        < /&的ContentTemplate GT;
      < / ASP:的UpdatePanel>
  < / DIV>
  < /表及GT;
< /身体GT;
< / HTML>

_ScriptManager.js (这里我只是添加一些动态内容使用id = divDyn跨度):

 函数动态(){
  警报(动态);
  $('#divDyn)文本。('动态!');
}

_ScriptManager.aspx.cs code-背后:

 公共部分类ScriptManagerTest:System.Web.UI.Page
{
    保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
        btnFirst.Click + = {委托mv.SetActiveView(VW1); };
        btnSecond.Click + = {委托mv.SetActiveView(VW2); };        如果(!的IsPostBack)
        {
            //测试1:不工作
            mv.SetActiveView(VW1);            //测试2:作品,因为所需的脚本加载初始页面请求
            //mv.SetActiveView(vw2);
        }
    }    保护覆盖无效在preRender(EventArgs的发送)
    {
        base.On preRender(E);        如果(mv.GetActiveView()== VW2)
        {
            //不调用RegisterClientScriptInclude
            //使得在两种情况下,警报的工作,但是,这是
            //什么它是所有关于:包括脚本,只有当
            //需要!
            ScriptManager.RegisterClientScriptInclude(
                页面,Page.GetType(),包括-JS
                ResolveClientUrl(〜/ ScriptManager.js));
            ScriptManager.RegisterStartupScript(
                对此,的GetType(),叩动,
                警报('你好');动态();,真正的);
        }
    }
}


解决方案

那么,这是拆东墙补西墙:后调查问题,OUF尝试所有的的ScriptManager的可能性两个小时的会议,试图使用jQuery,而不是最后写了这个样本(因为现实世界中的设置是一如既往更复杂),我现在发现的这短短的博客帖子解决了我的问题 - 添加了一行JS文件包括:

_ScriptManager.js

 函数动态(){
  警报(动态);
  $('#divDyn)文本。('动态!');
}
//通知脚本已经加载< - 新!
如果(!typeof运算(系统)==未定义)Sys.Application.notifyScriptLoaded();

好吧,也许这将是任何使用其他任何人在那里的......

I've read through the net but haven't found a solution to the following problem. I have this example page (_ScriptManager.aspx) with a ScriptManager, an UpdatePanel, a MultiView with two Views and two LinkButtons two switch between the views. The second view contains some functionality which I want to load a JavaScript file for (_ScriptManager.js).

Since I don't know whether the user will ever visit view 2 I don't want to include the javascript file statically on for every request. I only want to load it if and when I need it. So I need to include the script file during an asynchronous postback which is what I use ScriptManager.RegisterClientScriptInclude for. The pain is: it does not work. The script include is somehow not executed on the client so that the javascript functions inside cannot be used. Even worse, the script block I register in ScriptManager.RegisterStartupScript does not get executed in this case! This is all very irritating. The interesting thing is that the include script and the script block do get sent to the client with the async postback (Fiddler is my friend :-)) and the script file is also loaded. But then the javascript somehow seems to break...

Does anyone have a clue or know of a reported 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 (here I just add some dynamic content to the span with id=divDyn):

function dynamic() {
  alert('dynamic');
  $('#divDyn').text('Dynamic!');
}

_ScriptManager.aspx.cs code-behind:

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);    
        }
    }
}

解决方案

Well, this is shattering: after two 2h sessions of investigating the problem, trying ouf ALL of the possibilities of the ScriptManager, trying to use jQuery instead and finally writing up this sample (because the real world setup is as always more complicated), I now found this short blog post which solves my problem - with a single line added to the js file included:

_ScriptManager.js:

function dynamic() {
  alert('dynamic');
  $('#divDyn').text('Dynamic!');
}
// notify that the script has been loaded <-- new!
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

Well, maybe this will be of any use for anyone else out there...

这篇关于ScriptManager.RegisterClientScriptInclude不UpdatePanel的工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆