AJAX加载后注册的JavaScript函数(使用的UpdatePanel) [英] Register javascript function after AJAX load (using UpdatePanel)

查看:171
本文介绍了AJAX加载后注册的JavaScript函数(使用的UpdatePanel)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

< ASP:UpdatePanel的> (在我的ASP.NET(4.0)Web应用程序),我有HTML和JavaScript的变量/函数...例如,沿着这行的东西...

In an <asp:UpdatePanel> (in my ASP.NET (4.0) web application) I have both HTML and javascript variables/functions... for example, something along the lines of this...

请注意的JavaScript / HTML是,只有当在UpdatePanel通过一个AJAX更新更新显示...它没有在初始页面加载显示

Note that the javascript/HTML is only to be displayed when the UpdatePanel is updated via an AJAX update... it is not displayed on initial page load

<asp:UpdatePanel runat="server" id="udpMyPanel">
  <ContentTemplate>
    <%If Page.IsPostBack Then%>
    <script type="text/javascript">
      var myVar = "<%=CodeBehindVariable.ToString()%>";
      function myFnc() {
        ...
      }
    </script>
    <a href="javascript:void(0);" onclick="myFnc();return false;">Hello World</a>
    <%End If%>
  </ContentTemplate>
</asp:UpdatePanel>

的问题是,AJAX的updatePage加载时,在面板中的JavaScript是不可用的浏览器...因此调用 myFnc()不起作用的,因为该功能未注册。也不是 myVar的变量。

The problem is that when the UpdatePage AJAX is loaded, the javascript within the panel is not available to the browser... so calling myFnc() doesn't work, because the function isn't registered. Nor the myVar variable.

没有把整个脚本转换为字符串,并注册其与 ScriptManager.RegisterStartupScript 在codebehind,有没有什么办法来注册基于这种加价JavaScript的,一旦面板已加载浏览器?

Without putting the entire script into a string, and registering it with ScriptManager.RegisterStartupScript in the codebehind, is there any way to register this mark-up based javascript once the panel has been loaded by the browser?

推荐答案

这不是很好的,它不是pretty的,但是这个工程...

It's not nice, it's not pretty, but this works...

首先,我裹在&LT的JavaScript,ASP:面板&gt; 元素

<asp:UpdatePanel runat="server" id="udpMyPanel">
  <ContentTemplate>
    <%If Page.IsPostBack Then%>
    <asp:Panel runat="server" id="pnlJavascript">
      <script type="text/javascript">
        var myVar = "<%=CodeBehindVariable.ToString()%>";
        function myFnc() {
          ...
        }
      </script>
    </asp:Panel>
    <a href="javascript:void(0);" onclick="myFnc();return false;">Hello World</a>
    <%End If%>
  </ContentTemplate>
</asp:UpdatePanel>

然后在 preRender 在$ C $落后C,我抓住了面板的内容,去掉了HTML标签,并将其注册为脚本。然后将面板已被HIDEN因为没有一点被传递到浏览器。

Then on the PreRender in the code behind, I've captured the contents of the panel, removed the HTML tags, and registered it as script. Then the panel has been hiden as there is no point that being passed to the browser.

   Private Sub Page_PreRender(sender As Object, e As System.EventArgs) Handles Me.PreRender
        Dim sManager As ScriptManager = ScriptManager.GetCurrent(Me.Page)
        If sManager IsNot Nothing AndAlso sManager.IsInAsyncPostBack Then
            ' If AJAX postback, hide the DIV and use its content as the
            Dim sb As New System.Text.StringBuilder
            pnlJavascript.RenderControl(New HtmlTextWriter(New System.IO.StringWriter(sb)))
            Dim script As String = sb.ToString()

            ' Remove div tags (generated from the panel)
            Dim startTag As String = String.Format("<div id=""{0}"">", pnlJavascript.ClientID)
            script = script.Replace(startTag, "").Replace("</div>", "")

            ' Remove script tags
            script = script.Replace("<script type=""text/javascript"">", "").Replace("</script>", "")

            ' Register it
            ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType(), "registerJS", script, True)

            ' Hide it
            pnlJavascript.Visible = False
        End If
    End Sub

这篇关于AJAX加载后注册的JavaScript函数(使用的UpdatePanel)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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