Hi
I have below Modal. When there is error in Code behind Modal goes off. It should remain on Modal Popup Screen
Code
Add/Update Employee Category
×
Employee Category *
'>
Status
Active
Blocked
CloseC#
protected void btnAdd_Click(object sender, EventArgs e)
{
string errMessage = "";
if (String.IsNullOrWhiteSpace(txtDescription.Text))
{
errMessage += "Description is required and cannot Be empty.";
}
Boolean IsValidDescription = Common.CommonFunction.IsAlpha(txtDescription.Text);
if (IsValidDescription == false)
{
errMessage += "Description : Only Alphabets , Space , _ - allowed.";
}
}Thanks

Naimish MakwanaRank #324 · 7.7K reputationPosted Aug 27, 2024, 5:58 AM EDT
To ensure that the modal remains open when there’s an error in your code-behind, you can use a combination of client-side and server-side code. Here’s a step-by-step approach:
Add a Hidden Field to Store Error Messages: This will help in passing error messages from the server to the client.
Modify the Code-Behind to Set the Error Message: Update your
btnAdd_Clickmethod to set the error message in the hidden field.Use JavaScript to Check for Errors and Display the Modal: Add a script to check the hidden field for errors and display the modal if any errors are found.
Here’s how you can implement these steps:
1. Add a Hidden Field to Store Error Messages
Add a hidden field inside your modal to store error messages.
2. Modify the Code-Behind to Set the Error Message
Update your
btnAdd_Clickmethod to set the error message in the hidden field.protected void btnAdd_Click(object sender, EventArgs e) { string errMessage = ""; if (String.IsNullOrWhiteSpace(txtDescription.Text)) { errMessage += "Description is required and cannot be empty. "; } Boolean IsValidDescription = Common.CommonFunction.IsAlpha(txtDescription.Text); if (IsValidDescription == false) { errMessage += "Description: Only Alphabets, Space, _ - allowed. "; } if (!string.IsNullOrEmpty(errMessage)) { hfErrorMessage.Value = errMessage; ScriptManager.RegisterStartupScript(this, this.GetType(), "ShowModal", "$('#modal_form_horizontal').modal('show');", true); } else { // Your code to save the data } }3. Use JavaScript to Check for Errors and Display the Modal
Add a script to check the hidden field for errors and display the modal if any errors are found.
$(document).ready(function () { var errorMessage = ''; if (errorMessage) { $('#modal_form_horizontal').modal('show'); // Optionally, display the error message to the user alert(errorMessage); } });Complete Modal Code with Hidden Field and Script
Here’s how your complete modal code will look with the hidden field and script added:
Add/Update Employee Category × Employee Category * '> Status Active Blocked Close $(document).ready(function () { var errorMessage = ''; if (errorMessage) { $('#modal_form_horizontal').modal('show'); alert(errorMessage); } });Ramco RamcoRank #462 · 5.4K reputationPosted Aug 27, 2024, 11:11 AM EDT
Hi Naimish
It is giving error -
A potentially dangerous Request.Form value was detected from the client (hfErrorMessage="...- allowed.").
Description: ASP.NET has detected data in the request that is potentially dangerous because it might include HTML markup or script. The data might represent an attempt to compromise the security of your application, such as a cross-site scripting attack. If this type of input is appropriate in your application, you can include code in a web page to explicitly allow it. For more information, see http://go.microsoft.com/fwlink/?LinkID=212874.
Exception Details: System.Web.HttpRequestValidationException: A potentially dangerous Request.Form value was detected from the client (hfErrorMessage="...- allowed.").
Thanks
Naimish MakwanaRank #324 · 7.7K reputationPosted Aug 28, 2024, 12:15 AM EDT
This error occurs when ASP.NET detects potentially dangerous content in a form submission, such as HTML or script tags, which could be used for cross-site scripting (XSS) attacks. Here are a few ways to handle this:
ValidateRequest Attribute: You can disable request validation for a specific page by setting
ValidateRequest="false"in the page directive. However, this should be used with caution as it disables validation for the entire page.Web.config Settings: You can disable request validation for the entire application by modifying the
web.configfile. Again, this should be done carefully.HTML Encoding: Ensure that any user input is properly encoded before being displayed. This helps prevent XSS attacks by converting potentially dangerous characters into their HTML-encoded equivalents.
Thanks
Ramco RamcoRank #462 · 5.4K reputationPosted Aug 28, 2024, 12:19 AM EDT
hi Naimish
How the 3 step can be implemented . I don't want Validations to be disabled.
Thanks
Jignesh KumarRank #91 · 22.9K reputationPosted Aug 28, 2024, 12:31 AM EDT
Hello Ramco,
You need to handle erroe message as below,
Code behind :
protected void btnAdd_Click(object sender, EventArgs e) { string errMessage = ""; if (String.IsNullOrWhiteSpace(txtDescription.Text)) { errMessage += "Description is required and cannot be empty. "; } bool IsValidDescription = Common.CommonFunction.IsAlpha(txtDescription.Text); if (!IsValidDescription) { errMessage += "Description: Only Alphabets, Space, _, and - are allowed. "; } // Check error message and if any error can register model and keep model open using show if (!string.IsNullOrEmpty(errMessage)) { lblErrorMessage.Text = errMessage; // Prevent the modal from closing ScriptManager.RegisterStartupScript(this, GetType(), "ShowModal", "$('#modal_form_horizontal').modal('show');", true); } else { // Clear error message and proceed lblErrorMessage.Text = ""; // Your processing logic here } }