1

I am having problems getting a modal Bootstrap window to open up from server-side ASP.NET code, despite having followed all of the examples I find here. The code executes, and I get no JavaScript errors, but the modal window never appears.

Here is the modal window 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>

Here is the Javascript code to open the modal:

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

and finally, here's the code on the server-side meant to open the modal:

        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 );

        }

The goal is that when the user tries to sign in on the login page and is unsuccessful, the modal should pop up with a message. As I said, all of the code executes as written, but it still doesn't let the modal window actually come up in the browser. Help?

RiverNet
  • 121
  • 1
  • 6
  • Have you checked to console for errors? If so what are they? – Jon P Jan 31 '21 at 22:43
  • There are no errors of any kind in the browser console or on the .NET side. It's a "silent fail" of some kind and that's the confusing part. – RiverNet Jan 31 '21 at 23:22
  • 1
    There is a chance your RegisterStartUp script is running before the bootstrap js libraries are loaded (but I would have expected a console error in that case). Add a `console.log($("#StatusPopUp"))` at the start of the show method, to make sure that the modal element is loaded at that point. Am I correct in assuming this is a webforms project? – Jon P Jan 31 '21 at 23:37

1 Answers1

1

If the display of the modal is only triggered from a server side response on a page reload, I'd refactor this a little, using an asp:panel and moving the javascript into your regular jquery $(document).ready() function.

This way the modal HTML is not even rendered until it is needed and you have finer control of when the script to show the modal is called. You don't have to juggle any external script loading etc.

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");
});
Jon P
  • 19,442
  • 8
  • 49
  • 72
  • That works, Jon P! (smile) Seems a bit inelegant in a way, but it's hard to argue with what works, right? Thanks! I voted it as the answer, but I am too new for it to show up. – RiverNet Feb 01 '21 at 00:30
  • I think as a newcomer to StackOverflow you have to wait for 24hours to accept answers. You do lose a bit of elegance but you do gain more control of when your javascript executes. You also save your users a few bytes, by not sending them the HTML until they need it. Pannels are a great way to conditionally render content on a page based on server-side conditions. – Jon P Feb 01 '21 at 00:36