Hi
I have below Modal. When there is error in Code behind Modal goes off. It should remain on Modal Popup Screen
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
Jignesh KumarPosted Aug 28, 2024, 4:31 AM
Hello Ramco,
You need to handle erroe message as below,
Code behind :
Ramco RamcoPosted Aug 28, 2024, 4:19 AM
hi Naimish
How the 3 step can be implemented . I don't want Validations to be disabled.
Thanks
Naimish MakwanaPosted Aug 28, 2024, 4:15 AM
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 RamcoPosted Aug 27, 2024, 3:11 PM
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 MakwanaPosted Aug 27, 2024, 9:58 AM
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.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.
Complete Modal Code with Hidden Field and Script
Here’s how your complete modal code will look with the hidden field and script added: