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.
protected void btnAdd_Click(object sender, EventArgs e)
{
string errMessage = "";
if (String.IsNullOrWhiteSpace(txtDescription.Text))
{
errMessage += "Description is required and cannot Be empty.";
}
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
Jithu ThomasPosted Aug 25, 2024, 12:05 PM
Add a Hidden Field in Your ASPX Page: This hidden field will be used to store the state of the modal (whether it should be shown or not).
Add JavaScript to Check the Modal State: Use JavaScript to read the hidden field value and show the modal if necessary. Add this script at the bottom of your ASPX page.
Modify the Server-Side Code: In your
btnAdd_Clickmethod, set the hidden field totrueif there's an error. This will signal the client-side script to keep the modal open.Ensure Proper Scripts and References: Make sure you have included jQuery and Bootstrap JS references in your ASPX page to enable modal operations and DOM manipulations.