1

Estoy tratando de mostrar un mensaje de bootstrap al momento de inserter datos en mi base de datos pero al dar click se realiza un postback y el modal desaparece...

<div class="container">
            <div class="modal fade" id="user_Modal" role="dialog">
                <div class="modal-dialog">
                    <!-- Modal content-->
                    <div class="modal-content">
                        <div class="modal-header" style="padding: 35px 50px;">
                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                            <h3><span class="glyphicon glyphicon-user"></span>&nbsp New Usuario</h3>
                        </div>
                        <div class="modal-body" style="padding: 40px 50px;">
                            <div class="row">


                                Many others Div's

                                <!-- /.col-lg-6 -->
                                <div class="col-lg-6">
                                    <div class="input-group">
                                        <asp:Button ID="Button1" runat="server" CausesValidation="true" ValidationGroup="User_group" class="btn btn-success" Text="Guardar" OnClick="Btn_submitData" />
                                    </div>
                                    <br />
                                </div>
                                <!-- /.row -->
                                <div class="col-lg-6" id="alertSuccess" runat="server" style="display:none">
                                    <div class="alert alert-success">
                                        <strong>Success!</strong> The user has been insert.
                                    </div>
                                </div>

                                <div class="col-lg-6" id="alertFail" runat="server" style="display:none">
                                    <div class="alert alert-danger">
                                        <strong>Fail!</strong> Something went wrong, please try again.
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

Este es mi codigo

protected void Btn_submitData(object sender, EventArgs e)
        {
            String myConnectionString = @"C:\Users\gutiece\Desktop\" + "tool_track.accdb";

            using (OleDbConnection connection = new OleDbConnection())
            {
                using (OleDbCommand command = new OleDbCommand())
                {
                    connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data source= " + myConnectionString;
                    command.Connection = connection;
                    connection.Open();

                    command.Parameters.AddWithValue("@WHO", Txt_who.Text.Trim());
                    command.Parameters.AddWithValue("@PAYROLL_NUMBER", Convert.ToInt32(Txt_pay.Text));
                    command.Parameters.AddWithValue("@NAME", Txt_name.Text.Trim());
                    command.Parameters.AddWithValue("@OU", Txt_ou.Text.Trim());
                    command.Parameters.AddWithValue("@POOL", Convert.ToInt32(Txt_pool.Text));
                    command.Parameters.AddWithValue("@TEAM", Convert.ToInt32(Txt_team.Text));
                    command.Parameters.AddWithValue("@PASSWORD", Txt_pass.Text.Trim());
                    command.Parameters.AddWithValue("@ROL", Txt_rol.Text.Trim());

                    try
                    {
                        command.CommandText = "INSERT INTO users (who, payroll_number, name, ou, pool, team, pass, rol) VALUES ([@WHO], [@PAYROLL_NUMBER], [@NAME], [@OU], [@POOL], [@TEAM],[@PASSWORD], [@ROL])";
                        command.ExecuteNonQuery();
                        alertSuccess.Visible = true;
                    }
                    catch (Exception)
                    {
                        alertFail.Visible = true;
                    }
                    connection.Close();
                }
            }
        }

1 respuesta 1

1

Puedes dejar un campo tipo hidden con un valor = 1 para que en cuanto se recargue la pagina se muestre el modal:

Ejemplo:

protected void Btn_submitData(object sender, EventArgs e)
    {
        String myConnectionString = "C:\\Users\\gutiece\\Desktop\\" + "tool_track.accdb";

        using (OleDbConnection connection = new OleDbConnection())
        {
            using (OleDbCommand command = new OleDbCommand())
            {
                connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data source= " + myConnectionString;
                command.Connection = connection;
                connection.Open();

                command.Parameters.AddWithValue("@WHO", Txt_who.Text.Trim());
                command.Parameters.AddWithValue("@PAYROLL_NUMBER", Convert.ToInt32(Txt_pay.Text));
                command.Parameters.AddWithValue("@NAME", Txt_name.Text.Trim());
                command.Parameters.AddWithValue("@OU", Txt_ou.Text.Trim());
                command.Parameters.AddWithValue("@POOL", Convert.ToInt32(Txt_pool.Text));
                command.Parameters.AddWithValue("@TEAM", Convert.ToInt32(Txt_team.Text));
                command.Parameters.AddWithValue("@PASSWORD", Txt_pass.Text.Trim());
                command.Parameters.AddWithValue("@ROL", Txt_rol.Text.Trim());

                try
                {
                    command.CommandText = "INSERT INTO users (who, payroll_number, name, ou, pool, team, pass, rol) VALUES ([@WHO], [@PAYROLL_NUMBER], [@NAME], [@OU], [@POOL], [@TEAM],[@PASSWORD], [@ROL])";
                    command.ExecuteNonQuery();
                    alertSuccess.Visible = true;
                }
                catch (Exception)
                {
                    alertFail.Visible = true;
                }

                //Campo de tipo hidden que debe existir en tu asp
                Txt_hidden_mostrarModal.Text = "1";
                connection.Close();
            }
        }
    }

Entonces en el lado de JAVASCRIPT puedes utlizar jquery y validar en document ready:

$( document ).ready(function() {
      var mostrarModal = $("#Txt_hidden_mostrarModal").text() == "1";//Debes asegurar que el ID del hidden sea Txt_hidden_mostrarModal
      if(mostrarModal){
          $("#modal").show();//tendrias que poner tu codigo para mostrar el modal, este es solo un ejemplo
     }
});

Otra Posible solución

Otra forma de ahorrarte todo esto es llamando AJAX en lugar de SUMIT

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.