Open Modal bootstrap from code-behind

0

I need to open a modal when the password or user is incorrect, it will stay in Else .. however, it 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 the JavaScript code inside the

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

Mod 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>
    
asked by anonymous 30.09.2015 / 17:43

1 answer

1

This code does not work because the ScriptRegister is registering a Startup script, ie when the page loads for the first time, as this has already passed the script does not run. 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 the HTML is the modal like this:

<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 that ID.

In jquery the code is:

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

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

Switch to:

$('#modalLogin').modal('show');
    
30.09.2015 / 18:30