Ramco Ramco

Ramco Ramco

  • 442
  • 3.4k
  • 516.6k

Modal Popup

Aug 25 2024 11:18 AM

Hi

  I habe Modal and some validations at Server Side. If there is error it displays message but MOdal goes off. User has to enter all the information again.

<div id="modal_form_horizontal" class="modal fade">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header bg-info">
                <h5 class="modal-title">Add/Update Employee Category</h5>
                <button type="button" class="close" data-dismiss="modal">&times;</button>&nbsp;
            </div>

            <div class="form-horizontal">
                <div class="modal-body">
                    <div class="row">
                        <div class="col-lg-6">
                            <div class="form-group">
                                <label>Employee Category</label>&nbsp;<span style="color: red">*</span>
                                <asp:TextBox ID="txtDescription" MinLength="3" MaxLength="50" class="form-control" required="true" runat="server" Text='<%# Eval("description") %>'></asp:TextBox>
                            </div>
                        </div>

                    </div>
                </div>

                <div class="modal-footer">
                    <button type="button" class="btn btn-link" data-dismiss="modal">Close</button>
                    <asp:Button ID="lnkEmpCategory" runat="server" class="btn bg-primary" OnClick="btnAdd_Click" Text="Save" />&nbsp;
                </div>
            </div>
        </div>
    </div>
</div>
protected void btnAdd_Click(object sender, EventArgs e)
{
    string errMessage = "";

    if (String.IsNullOrWhiteSpace(txtDescription.Text))
    {
        errMessage += "Description is required and cannot Be empty.</br>";
    }

    if (errMessage == "")
    {
        try
        {
            if (hdfId.Value == "0")
            {
                using (SqlConnection con = new SqlConnection(Common.CommonFunction.cnn_Live))
                {
                    SqlCommand cmd = new SqlCommand("sp_EmployeeCategory", con);
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.AddWithValue("@Action", "I");
                    cmd.Parameters.AddWithValue("@Description", SqlDbType.VarChar).Value = txtDescription.Text.ToUpper();
                    //cmd.Parameters.AddWithValue("@ShortName", SqlDbType.VarChar).Value = txtShortName.Text.ToUpper();
                    cmd.Parameters.AddWithValue("@CreatedBy", SqlDbType.Int).Value = (hdfLoginId.Value);
                    cmd.Parameters.AddWithValue("@UpdatedBy", SqlDbType.Int).Value = (hdfLoginId.Value);
                    SqlParameter successParam = cmd.Parameters.Add("@Success", SqlDbType.Bit);
                    successParam.Direction = ParameterDirection.Output;
                    con.Open();

                    cmd.ExecuteNonQuery();
                    bool success = (bool)successParam.Value;
                    if (success)
                    {
                        string message = Common.CommonFunction.recordInsertedSucessfully;
                        ShowMessage("Success", message, "Success");
                    }
                    else
                    {
                        ShowMessage("Oops...", success.ToString(), "error");
                    }
                    GetData();
                }
            }
            else
            {
                using (SqlConnection con = new SqlConnection(Common.CommonFunction.cnn_Live))
                {
                    SqlCommand cmd = new SqlCommand("sp_EmployeeCategory", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Action", "U");
                    cmd.Parameters.AddWithValue("@DocEntry", SqlDbType.Int).Value = (hdfId.Value);
                    cmd.Parameters.AddWithValue("@Description", SqlDbType.VarChar).Value = txtDescription.Text.ToUpper();

                    SqlParameter successParam = cmd.Parameters.Add("@Success", SqlDbType.Bit);
                    successParam.Direction = ParameterDirection.Output;
                    con.Open();

                    cmd.ExecuteNonQuery();
                    bool success = (bool)successParam.Value;
                    if (success)
                    {
                        string message = Common.CommonFunction.recordUpdatedSucessfully;
                        ShowMessage("Success", message, "Success");
                    }
                    else
                    {
                        ShowMessage("Oops...", success.ToString(), "error");
                    }
                    GetData();
                }
            }
        }
        catch (Exception ex)
        {
            ShowMessage("Oops...", Common.CommonFunction.ErrorMessage, "error");
        }
    }
    else
    {
        if (errMessage == "")
        {
            ShowMessage("Oops...", Common.CommonFunction.ErrorMessage, "error");
            GetData();
        }
        else
        {
            ShowMessage("Oops...", errMessage, "error");
            GetData();
        }
    }
}

Thanks


Answers (1)