如何在部分回发上保留脚本块? [英] How to retain script block on a partial postback?

查看:125
本文介绍了如何在部分回发上保留脚本块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正在处理的webapp中遇到的一个问题。

因此,我没有使用不相关的代码混淆问题,而是在一个孤立的,简化的webapp中重新创建了问题演示了这个问题。

希望这有助于找到解决方案。

This is a problem I'm facing in a webapp I'm currently working on.
So instead of cluttering the question with unrelated code, I recreated the problem in an isolated, simplified webapp that only demonstrates this issue.
Hopefully this helps in finding a solution.

我有一个网络用户控件,其中包含以下内容:

I have a web user control with just this as its content:

<% if (ShowAlertScript)
   { %>
       <script type="text/javascript">
           function AlertMe() 
           {
               alert('Hello World!');
           }
       </script>
<% } %>
<input type="button" onclick="AlertMe()" value="Say Hello" />

它的代码隐藏只不过是 ShowAlertScript的布尔定义

这表示我在大型网络应用程序中拥有的控件有两种模式:输入模式显示模式。在输入模式时,它有一个大的javascript块,只有在那时才有用;它做了一些很酷的东西来帮助用户输入信息。

And its codebehind has nothing more than the boolean definition of ShowAlertScript.
This represents a control I have in the big webapp that has two modes: input mode, and display mode. When in input mode, it has a large javascript block that is only useful then; it does some cool stuff to help the user input the info.

此控件在大图中的布局如下:

The way this control is laid out in the big picture is as follows:

<asp:ScriptManager runat="server" />
<asp:UpdatePanel runat="server">
  <ContentTemplate>
    <asp:MultiView ActiveViewIndex="0" runat="server" ID="mvw">
      <asp:View runat="server">
        <asp:Button runat="server" ID="btnSwitch" 
            OnClick="btnSwitch_Click" Text="Switch" />
      </asp:View>
      <asp:View runat="server">
        <uc:MyInputControl runat="server" ID="micInput" ShowAlertScript="true" />
      </asp:View>
    </asp:MultiView>
  </ContentTemplate>
</asp:UpdatePanel>

点击 btnSwitch 时,只需切换使用用户控件到第二个视图。
注意我如何将 ShowAlertScript 初始化为 true

潜力输出是因为我在用户控件中显示警报脚本,当您单击输入时,将执行 AlertMe()函数按钮元素,因为它是根据内联 if 语句写出来的。

When you click btnSwitch, it simply switches to the second view with the user control. Notice how I have the ShowAlertScript already initialized to true.
The potential output is that since I'm "showing the alert script" within the user control, the AlertMe() function will execute when you click the input-button element, because it's written out according to the inline if statement.

如果你运行这个我给你的代码,它将无法正常工作。

浏览器会说它看不到 AlertMe()功能;据它所知,它是未定义的。
但是如果你拿出 UpdatePanel (或者禁用 ScriptManager 的部分渲染),它将会单击 btnSwitch 时,在完整回发时工作得很好。

If you run this code I gave you so far as is, it will not work.
The browser will say it can't see the AlertMe() function; it's undefined as far as it knows. But if you take out the UpdatePanel (or disable the ScriptManager's partial rendering), it will work just fine on a full postback when you click btnSwitch.

我希望它能用于部分回发,因为与页面的其他部分相比,整个事情是一小部分,我不想做每次切换视图时都会进行完全回发。

显然, ScriptManager 甚至无法重新呈现ascx文件以进行可能的更改。

ScriptManager 不够智能,或者有一个我缺少的选项让它呈现< script> 所以我可以在客户端调用它的方法。

I want it to work on a partial postback, because this whole thing is a small piece compared to the rest of the page, and I don't want to do a full postback every time they switch views.
Apparently, the ScriptManager doesn't even bother re-rendering the ascx file for possible changes.
Either the ScriptManager is not smart enough, or there is an option I'm missing to let it render the <script> so I can invoke its methods on the client side.

有人可能建议的一个潜在答案是你为什么不拿出javascript,把它放在自己的.js文件中,并让页面引用它所以它是可用于控件吗?

这对我来说无法真正起作用,因为脚本块会对该单个控件实例进行一些初始化和修改,而不是对页面上的所有其他实例进行初始化和修改。

A potential answer someone might suggest is "why don't you take out the javascript, put it in its own .js file, and have the page reference it so it's available to the control?"
That won't really work with me, because the script block does some initialization and modification that pertain to that single control instance, not to all others on the page.

反过来,您可能还会担心如果我有多个控件实例,我最终会得到相同脚本的副本。并不是的;在任何给定的时间内,页面中只有一个输入模式,这只是我在两个单独的视图中有这两种模式,我让用户在它们之间切换。

In turn, you might also be concerned that if I have more than one instance of the control, I end up with copies of the same script. Not really; there will only be ONE input mode of this control in a page at any given time, it's just that I have those two modes in two separate views and I let the user switch between them.

感谢您阅读此内容=)

感谢任何帮助和输入。

Thanks for just reading this far =)
Appreciate any help and input.

推荐答案

为您的脚本块正确支持 partial页面呈现,您必须使用 PreRender 阶段注册它/en-us/library/bb350750.aspx\"rel =nofollow> ScriptManager.RegisterClientScriptBlock()方法:

For your script block to properly support partial page rendering, you have to register it during the PreRender phase of your user control, using the ScriptManager.RegisterClientScriptBlock() method:

protected void Page_PreRender(object sender, EventArgs e)
{
    if (ShowAlertScript) {
        ScriptManager.RegisterClientScriptBlock(this,
            typeof(YourUserControl), "AlertScript",
            @"function AlertMe() 
              {
                  alert('Hello World!');
              }",
            true);
    }
}

(以上假设您拥有 属性设置为 true 你的 ascx 文件的d19c0t4b.aspxrel =nofollow> @ Control 指令。)

(The above assumes you have the AutoEventWireup attribute set to true in the @ Control directive of your ascx file.)

此外,由于脚本是从用户控件注册的,但您的 ScriptManager 驻留在页面本身上,您可能需要添加 < asp:ScriptManagerProxy> 元素到您的控制标记,以便上述工作。

Also, since the script is registered from a user control but your ScriptManager resides on the page itself, you'll probably have to add an <asp:ScriptManagerProxy> element to your control markup for the above to work.

这篇关于如何在部分回发上保留脚本块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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