使用JavaScript更改页面状态,回发时会调用旧状态



基本上,我们有一个通知消息框的"幻觉",它在MasterPage中以.Visible=false的形式存在。当需要在框中显示消息时,我们运行一个看起来像这样的方法:

public static void DisplayNotificationMessage(MasterPage master, string message)
{
   if (Master.FindControl("divmsgpanel") != null)
   {
      master.FindControl("divmsgpanel").Visible = true;
   }
   if (master.FindControl("divdimmer") != null)
   {
      master.FindControl("divdimmer").Visible = true;
   }
   TextBox thetxtbox = (TextBox)master.FindControl("txtboxmsgcontents");
   if (thetxtbox != null)
   {
      thetxtbox.Text = message;
   }

}

基本上,通过我们的设计师令人敬畏的CSS voodoo,我们最终得到了一个浮动的消息框,因为页面的其余部分看起来都变暗了。此消息框有一个"关闭"按钮,用于消除"弹出"并恢复调光器,使网站恢复到"正常"视觉状态。我们在MasterPage:中使用JavaScript实现这一点

    function HideMessage() {
        document.getElementById("<%# divmsgpanel.ClientID %>").style.display = 'none';
        document.getElementById("<%# divdimmer.ClientID %>").style.display = 'none';
        return false;
    }

并且按钮在.aspx页面中的声明调用这个HideMessage()函数OnClientClick:

    <asp:Button ID="btnmsgcloser" runat="server" Text="Close" style="margin: 5px;"
    OnClientClick="return HideMessage()" />

问题:

所有未来的回发都会导致MasterPage"记住"那些div在执行HideMessage()JavaScript之前的状态。换句话说,在DisplayNotificationMessage()方法的初始调用之后的每一次回发都会导致页面返回到divmsgpanel。Visible=true和divdimer。Visible=true,在每次回发时都会错误地弹出一个令人讨厌的消息框。

问题:

既然我们希望Close函数保持客户端JavaScript,那么对于这两个div,我们如何才能"通知"页面在回发时停止恢复到旧状态?

您可以尝试在Master_Page Load事件中将它们设置为Visible=false吗?它应该在您调用DisplayNotificationMessage 时隐藏并重新显示它们

最新更新