Show bootstrap message

1

I'm trying to show a bootstrap message when inserting data in my database but when clicking, a postback is made and the modal disappears ...

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

This is my code

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();
                }
            }
        }
    
asked by Cesar Gutierrez Davalos 18.07.2017 в 22:32
source

1 answer

1

You can leave a field type hidden with a value = 1 so that as soon as the page is reloaded the modal is shown:

Example:

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

Then on the JAVASCRIPT side you can use jquery and validate in 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
     }
});

Another Possible solution

Another way to save yourself all this is by calling AJAX instead of SUMIT

    
answered by 19.07.2017 в 07:56