繁体   English   中英

如何在使用UpdateProgress控件的UpdatePanel中设置控件的可见性?

[英]How to set visibility of a control within an UpdatePanel that uses an UpdateProgress control?

我在包含嵌套面板的页面上有一个更新面板。 它具有一个带有小型表单(如登录表单)的面板和一个按钮。 单击该按钮时,将检查一些条件,如果通过,则将隐藏第一个面板,并显示具有较大窗体的第二个面板。

因为较大的窗体可能需要一些时间来加载(它会从其他来源提取数据),所以较小的窗体面板包含一个UpdateProgress控件。

所有这些都很好。

然后,我在登录表单面板中的UpdateProgress下添加了一个嵌套面板,该面板将显示有意义的错误消息,例如,如果登录代码不正确。 这将按预期显示。 但是,如果用户随后更正了他们的信息并再次单击该按钮,则UpdateProgress会正确显示,但是即使我在后面的代码中尝试将其设置为Visible = false,错误消息仍然可见。

该页面不使用母版页。

调试显示Visible属性的确设置为false,尽管它仍显示在浏览器中。

ASPX代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="custom_Default" %>
<!DOCTYPE html>
<html lang="en">
    <head runat="server">
    <!-- meta and link tags -->
    </head>
    <body>
        <form id="form1" runat="server">
            <div class="container">
                <!-- h1 and all that -->
                <asp:ScriptManager ID="scriptManager" runat="server"></asp:ScriptManager>
                <asp:UpdatePanel ID="pnlUpdate" runat="server">
                    <ContentTemplate>
                        <asp:Panel ID="pnlLogin" runat="server">
                            <!-- label and text field -->
                            <asp:Button ID="btnBegin" runat="server" OnClick="btnBegin_Click" />
                             <asp:UpdateProgress ID="updProgress" runat="server" AssociatedUpdatePanelID="pnlUpdate">
                                  <ProgressTemplate>
                                      <p>Loading, please wait... </p>
                                  </ProgressTemplate>
                             </asp:UpdateProgress>
                             <asp:Panel ID="pnlMessage" runat="server" Visible="false">
                                <asp:Literal ID="ltlMessage" runat="server" />
                             </asp:Panel>
                        </asp:Panel>
                        <asp:Panel ID="pnlMainForm" runat="server" Visible="false">
                        </asp:Panel>
                        <asp:Panel ID="pnlConfirmation" runat="server" Visible="false">
                        </asp:Panel>
                    </ContentTemplate>
                </asp:UpdatePanel>
            </div>
        </form>
     <!-- javascript - jquery and bootstrap -->
     </body>
</html>

后台代码(我尝试在Page_Load,pnlUpdate_Load事件和btnBegin_Click事件中将visible设置为false,但是该错误消息不会消失。

protected void btnBegin_Click(object sender, EventArgs e)
{
     pnlMessage.Visible = false;
     Page.Validate();
     if (Page.IsValid)
     {
          // check some conditions. if fails:
          ltlMessage.Text = "Reason for failure";
          pnlMessage.Visible = true; // works
      }
}

我尝试从ASPX页面上的pnlMessage中删除Visible =“ false”,并将其放置在Page_Load中的代码中,但是在显示消息后,我仍然无法隐藏它。

第二次单击btnBegin后,如何隐藏pnlMessage面板?

我设法找到解决方案。 显然,.NET中没有方法可以执行-必须在JavaScript中完成。

这是我用来完成此操作的代码。

<script type="text/javascript">

    var prm = Sys.WebForms.PageRequestManager.getInstance();

    prm.add_initializeRequest(initializeRequest);
    prm.add_endRequest(endRequest);

    var _postBackElement;

    function initializeRequest(sender, e) {
        if (prm.get_isInAsyncPostBack()) {
            e.set_cancel(true);
        }

        $get('pnlMessage').style.display = 'none';
    }

    function endRequest(sender, e) {
        $get('pnlMessage').style.display = 'block';
    }
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM