Hi
I am getting error on this line
ScriptManager.RegisterStartupScript(this, this.GetType(), "ShowModal", "$('#modal_form_horizontal').modal('show');", true);
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.
Exception Details: System.Web.HtpRequestValidationException: A potentially dangerous Request.Form value was detected from the client (hfErrorMessage="...- allowed.").
protected void btnSubmit_Click(object sender, EventArgs e)
{
string errMessage = "";
if (String.IsNullOrWhiteSpace(txtBankName.Text))
{
errMessage += "Bank Name 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_Bank", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Action", "I");
cmd.Parameters.AddWithValue("@BankCode", SqlDbType.VarChar).Value = txtBankCode.Text.ToUpper();
cmd.Parameters.AddWithValue("@BankName", SqlDbType.VarChar).Value = txtBankName.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.recordInsertedSucessfully;
ShowMessage("Success", message, "Success");
}
else
{
ShowMessage("Oops...", success.ToString(), "error");
}
}
}
}
catch (Exception ex)
{
ShowMessage("Oops...", Common.CommonFunction.ErrorMessage, "error");
}
}
else
{
if (errMessage == "")
{
ShowMessage("Oops...", Common.CommonFunction.ErrorMessage, "error");
}
else
{
hfErrorMessage.Value = errMessage;
ScriptManager.RegisterStartupScript(this, this.GetType(), "ShowModal", "$('#modal_form_horizontal').modal('show');", true);
}
}
}
Thanks
Naimish MakwanaPosted Aug 28, 2024, 4:48 AM
It sounds like the modal is showing, but the error message isn’t being displayed. To ensure the error message is displayed, you need to bind the
hfErrorMessagevalue to a control within the modal. Here’s how you can do it:1. Add a Label Control to Display the Error Message
Add a label inside your modal to display the error message:
2. Set the Error Message in the Code-Behind
Set the text of the
lblErrorMessagelabel in your code-behind:3. Ensure the Label is Updated
Make sure the label
lblErrorMessageis updated with the encoded error message before showing the modal. This way, the error message will be displayed correctly within the modal popup.This should ensure that the error message is displayed within the modal when it is shown.
Ramco RamcoPosted Aug 28, 2024, 4:30 AM
Hi Naimish
It remains on Modal Popup but error message is not getting displayed.
Thanks
Naimish MakwanaPosted Aug 28, 2024, 4:20 AM
The error you’re encountering,
System.Web.HttpRequestValidationException, occurs because ASP.NET is detecting potentially dangerous content (like HTML or script) in the request. This is a security feature to prevent cross-site scripting (XSS) attacks.To resolve this issue, you can either:
Option 1: Encode the Error Message
You can encode the error message before assigning it to
hfErrorMessageto ensure that any HTML tags are treated as plain text. Here’s how you can do it:Option 2: Disable Request Validation (Not Recommended)
If you still want to disable request validation for this specific control, you can do so by setting the
ValidateRequestattribute tofalsein the page directive and using theValidateInputattribute in the method. However, this approach is not recommended due to security risks.Using the first option (encoding the error message) is generally safer and recommended to avoid potential security vulnerabilities.
Thanks