加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-ajax – ScriptManager.RegisterClientScriptInclude在U

发布时间:2020-12-16 04:25:22 所属栏目:asp.Net 来源:网络整理
导读:我已经通过网络阅读,但没有找到解决以下问题的方法.我有这个示例页面(_ScriptManager.aspx),带有ScriptManager,一个UpdatePanel,一个带有两个视图的MultiView和两个LinkBut??tons两个视图之间的切换.第二个视图包含一些我想为(_ScriptManager.js)加载 JavaSc
我已经通过网络阅读,但没有找到解决以下问题的方法.我有这个示例页面(_ScriptManager.aspx),带有ScriptManager,一个UpdatePanel,一个带有两个视图的MultiView和两个LinkBut??tons两个视图之间的切换.第二个视图包含一些我想为(_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代替并最终编写此示例(因为真实世界设置总是更复杂),我现在发现 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();

好吧,也许这对任何其他人都有用……

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读