无法从服务器端 ASP.NET 代码打开 Bootstrap 模式窗口

时间:2021-01-31 22:34:02

标签: asp.net twitter-bootstrap server-side-scripting

尽管遵循了我在此处找到的所有示例,但我仍然无法从服务器端 ASP.NET 代码中打开模式引导程序窗口。代码执行,我没有收到 JavaScript 错误,但模态窗口从未出现。

这是模态窗口 HTML:

    <div id="StatusPopUp" class="modal fade" role="dialog">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">
                        &times;</button>
                    <h4 class="modal-title">Credentials Not Found</h4>
                </div>
                <div class="modal-body">
                    <p class="text-justify">
                        The email address and/or password entered do not match our records.  Please try your login again if you believe this is an
                        error.
                    </p>
                    <p class="text-justify">
                        If you are an associate and need credentials for use of the Portal then please contact your regional office
                        or Team Sales Lead to request them.
                    </p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-danger" data-dismiss="modal">
                        Close</button>
                </div>
            </div>
        </div>
    </div>

这是打开模态的Javascript代码:

    <script type="text/javascript">
        function Show() {
            $("#StatusPopUp").modal("show");
        }
    </script>

最后,这是服务器端用于打开模态的代码:

        protected void btnGo_Click(object sender, EventArgs e) {
            bool isValid=Membership.ValidateUser(UName.Value,Pwd.Value);
            if ( isValid )
                this.Response.Redirect ( "associates/home.aspx", true );
            else
                ClientScript.RegisterStartupScript ( this.GetType ( ), "alert", "Show();", true );

        }

目标是当用户尝试在登录页面上登录但不成功时,模态应该弹出一条消息。正如我所说,所有代码都按照编写的方式执行,但它仍然没有让模式窗口真正出现在浏览器中。帮助?

1 个答案:

答案 0 :(得分:0)

如果模式的显示仅在页面重新加载时从服务器端响应触发,我会稍微重构一下,使用 asp:panel 并将 javascript 移动到您的常规 jquery $(document).ready()功能。

通过这种方式,模态 HTML 甚至不会在需要之前呈现,并且您可以更好地控制何时调用显示模态的脚本。您不必处理任何外部脚本加载等。

aspx

<asp:panel id="pnlStatusPopUp" runat="server" visible="false">
  <div id="StatusPopUp" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">
                    &times;</button>
                <h4 class="modal-title">Credentials Not Found</h4>
            </div>
            <div class="modal-body">
                <p class="text-justify">
                    The email address and/or password entered do not match our records.  Please try your login again if you believe this is an
                    error.
                </p>
                <p class="text-justify">
                    If you are an associate and need credentials for use of the Portal then please contact your regional office
                    or Team Sales Lead to request them.
                </p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-danger" data-dismiss="modal">
                    Close</button>
            </div>
        </div>
    </div>
  </div>
</asp:panel>

.cs

protected void btnGo_Click(object sender, EventArgs e) {
    bool isValid=Membership.ValidateUser(UName.Value,Pwd.Value);
    if ( isValid )
         this.Response.Redirect ( "associates/home.aspx", true );
    else
         pnlStatusPopUp.visible = true;
}

js

$(document).ready(function(){
   $("#StatusPopUp").modal("show");
});