Open Modal Bootstrap from code-Behind

Asked

Viewed 3,075 times

0

I need to open a modal when the password or user are incorrect, it will be on Else .. but does not open the modal

protected void bntLogar_Click(object sender, EventArgs e)
    {
        Registrar criptografia = new Registrar();

        if (Login.logarUsuario(txtUser.Text, criptografia.CriptografiaMD5(txtSenha.Text)))
        {
            //Cria um cookie do lado do servidor
            HttpCookie cookie = new HttpCookie("estado", "conectado");

            //Define a validade do cookie (10 dias a partir de hoje)
            cookie.Expires = DateTime.Now.AddMonths(12);

            //Envia o cookie para o cliente
            Response.Cookies.Set(cookie);

            //Redireciona para a pagina inicial
            Response.Redirect("Admin.aspx");

            //fortawsome
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "openModal();", true);
        }

    }

And I put inside the tag the Javascript code

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

Modal code

<div class="modal fade" id="modalLogin" runat="server" role="dialog">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4>Warning</h4>
                </div>
                <div class="modal-body">
                    <p>Watch out! Your about to be locked out.</p>
                </div>
                <div class="modal-footer">
                    <a class="btn btn-primary" data-dismiss="modal">Close</a>
                </div>
            </div>
        </div>
    </div>

1 answer

1


This code does not work because Scriptregister is registering a Startup script, that is how much the page loads for the first time, as it has already passed the script is not executed. The alternative would be to use:

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Pop", "openModal();", true);

Code for the Script manager

   <asp:ScriptManager runat="server"></asp:ScriptManager>

EDIT:

The last problem is jQuery calling the wrong id in HTML is the modal so:

<div class="modal fade" id="modalLogin" runat="server" role="dialog">

Note that the modal ID is "modalLogin" so all interactions that will be made with the modal will be by this ID.

In jquery the code is:

$('#myModal').modal('show');

Which essentially means: Find the HTML element with the "myModal" ID and convert it to a modal with the argument being "show". Since the modal ID is "modalLogin" and this calling the ID "myModal" nothing will happen

Switch to:

$('#modalLogin').modal('show');
  • Thanks for the feedback. I tested the code but it didn’t work.. still doesn’t show the modal when it doesn’t authenticate the user.

  • Does the page . aspx (layout or master) have an associated Scriptmanager? I edited the answer.

  • True, that was missing. I put logos after <form id="form1" runat="server"> but it didn’t work. I put the modal code in the post.. I really have no idea why it doesn’t work

  • I edited the answer again, I suppose that’s the final problem.

  • Thank you very much for your attention.. I applied the necessary corrections but it still doesn’t work.. nothing from modal open.. I really don’t know what’s going on.

  • Remove the "runat=server" tag from the modal, this changes the ID to an automatically generated one. Let’s hope it’s the last thing

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.