【问题标题】:Why does an UpdatePanel triggering cause all other content to disappear?为什么触发 UpdatePanel 会导致所有其他内容消失?
【发布时间】:2012-02-14 08:43:36
【问题描述】:

我正在编写一个 ASP.Net Web Forms 应用程序,它有几个缓慢的后端操作,所以我尝试使用 UpdatePanels 来异步执行这些操作。

我希望这些操作一次只能执行一项,并在有人执行其中一项操作时通知其他用户。作为一种快速而肮脏的方式,我想建立一个简单的模态对话框,后面有一个掩码,在操作发生时禁用对站点的访问。我在网站所有页面共享的 MasterPage 中执行此操作。

我将模式卡在 UpdatePanel 中,并在其上放置了一个计时器来检查全局变量(来自 ApplicationState)的当前状态。如果它正在执行任务,则显示模式,否则通过设置 Visibility = False 将其禁用。我只希望更新该内容,因此我在我的 ScriptManager 中确保 EnablePartialRendering="true" 并在 UpdatePanel 上设置 UpdateModel="Contitional"。我什至在 Timer's Tick 事件中调用 .Update()。

对我来说听起来不错。为了测试,我让模态随机打开和关闭。到目前为止,计时器工作得很好,模态显示和隐藏起来就像一个冠军。唯一的问题是,第二次隐藏模式后,一切都消失了。

所以...对于存在,模式是隐藏的,主页显示正常。然后计时器触发,显示模态,漂亮地覆盖主页。计时器再次触发,模态框消失了,但其他一切都消失了!

我在 Chrome 中通过“检查元素”检查了活动的 html 源代码,内容消失了!

我在这里遗漏了一些非常明显的东西......

这是我的代码:

    <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="OPTH.DesktopRollout.Website.SiteMaster" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head runat="server">
        <title></title>
        <link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
        <asp:ContentPlaceHolder ID="HeadContent" runat="server">
        </asp:ContentPlaceHolder>    
    </head>
    <body>
        <form runat="server">
            <asp:ScriptManager ID="MainSM" runat='server' EnablePartialRendering="True" />        

            <asp:Timer runat="server" ID="CheckTimer" Interval="500" OnTick="CheckTimer_Tick"></asp:Timer>

            <asp:UpdatePanel runat="server" ID="ModalUp" UpdateMode="Conditional" RenderMode="Block" >
                <Triggers>
                    <asp:AsyncPostBackTrigger controlid="CheckTimer" eventname="Tick" />
                </Triggers>
            <ContentTemplate>            
                <asp:Panel id="modalPnl" runat="server">
                    <div class="modalPopup">
                        <div id="modalContent">Currently performing task: '<asp:Literal runat="server" ID="TaskNameLtr" />'. Please wait till it has completed before continuing.</div>                
                    </div>
                    <div class="modalOverlay" />    
                </asp:Panel>                        
            </ContentTemplate>
            </asp:UpdatePanel>

    //... other content is here... just static html and a ContentPlaceHolder for basepage.

后端代码:

    protected void CheckTimer_Tick(object sender, EventArgs e)
    {

        modalPnl.Visible = IsExecutingTask; //Returns a random true or false based on time.
        TaskNameLtr.Text = DateTime.Now.ToShortDateTimeString();
        ModalUp.Update();
    }

老师!这只小蚱蜢做错了什么?

我不认为这与它有任何关系,但我的 CSS:

    .modalOverlay
    {
        position: fixed;
        width: 100%;
        height: 100%;    
        background-color: black;
        z-index: 1;

        filter: alpha(opacity=50); /* internet explorer */
        -khtml-opacity: 0.5;      /* khtml, old safari */
        -moz-opacity: 0.5;       /* mozilla, netscape */
        opacity: 0.5;           /* fx, safari, opera */     
    }

    .modalPopup 
    {
        position: fixed;
        margin-left: -225px;    
        top: 40%;
        left: 50%;
        width: 450px;
        height:100px;
        background-color: white;
        border: black solid 3px;
        z-index: 2;

        filter: alpha(opacity=100); /* internet explorer */
        -khtml-opacity: 1;      /* khtml, old safari */
        -moz-opacity: 1;       /* mozilla, netscape */
        opacity: 1;           /* fx, safari, opera */
    }

    .modalPopup #modalContent 
    {
        margin: 15px 30px;    
    }

有什么想法吗?

更新:我刚刚将所有 AJAX 代码从 MasterPage 移动到 Default.aspx。现在在异步回发中,Masterpage 中的内容仍然存在,但 Default.aspx 中的内容消失了。

【问题讨论】:

  • 我不得不使用 Session,因为更新面板会清除所有母版页变量

标签: asp.net-ajax updatepanel


【解决方案1】:

从关于这个问题的所有搜索中,我发现每次在处理不正确的 DIV 标签格式时都解决了这个问题。

this post.

【讨论】:

    【解决方案2】:

    您应该再次将控件添加到更新面板内的页面,否则母版页的加载功能会清除控件,因此这些控件的操作不再起作用

    【讨论】:

      【解决方案3】:

      就我而言,AsyncPostBackTrigger ControlID 不正确。 Timer 导致了回发,但具有条件模式的 UpdatePanel 实际上并未更新,因为 Timer 不是 UpdatePanel 的触发器。

      <Triggers>
          <asp:AsyncPostBackTrigger controlid="CheckTimer" eventname="Tick" />
      </Triggers>
      

      (我知道这是一个古老的帖子,但这是我在搜索结果中得到的问题。在这里发帖希望对某人有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-07
        • 2020-06-29
        • 2023-03-07
        • 2012-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-18
        相关资源
        最近更新 更多