Hi
I am concatenating error message like below. I want when Error message gets displayed . should not get displayed in text.
C#
if (String.IsNullOrWhiteSpace(txtBankName.Text))
{
errMessage += "Bank Name is required and cannot Be empty.";
}
Boolean IsValidDescription = Common.CommonFunction.IsAlpha(txtBankName.Text);
if (IsValidDescription == false)
{
errMessage += "Bank Name : Only Alphabets , Space , _ - allowed.";
}Thanks

Naimish MakwanaRank #324 · 7.7K reputationPosted Aug 28, 2024, 1:28 AM EDT
Or you can use like this
if (String.IsNullOrWhiteSpace(txtBankName.Text)) { errMessage += "Bank Name is required and cannot be empty." + Environment.NewLine; } Boolean IsValidDescription = Common.CommonFunction.IsAlpha(txtBankName.Text); if (IsValidDescription == false) { errMessage += "Bank Name: Only Alphabets, Space, _ - allowed." + Environment.NewLine; }Or try
if (String.IsNullOrWhiteSpace(txtBankName.Text)) { errMessage += "Bank Name is required and cannot be empty. "; } Boolean IsValidDescription = Common.CommonFunction.IsAlpha(txtBankName.Text); if (IsValidDescription == false) { errMessage += "Bank Name: Only Alphabets, Space, _ - allowed. "; }Thanks
Naimish MakwanaRank #324 · 7.7K reputationPosted Aug 28, 2024, 1:25 AM EDT
To ensure that the
tags are interpreted as line breaks in the error message rather than being displayed as text, you can use theInnerHtmlproperty of the label instead of theTextproperty. This way, the HTML tags will be rendered correctly.Here’s how you can modify your code:
1. Update the Label Control to Use
InnerHtmlEnsure your label control is set to use
InnerHtml:2. Set the Error Message Using
InnerHtmlIn your code-behind, set the
InnerHtmlproperty of the label:protected void btnSubmit_Click(object sender, EventArgs e) { string errMessage = ""; if (String.IsNullOrWhiteSpace(txtBankName.Text)) { errMessage += "Bank Name is required and cannot be empty."; } Boolean IsValidDescription = Common.CommonFunction.IsAlpha(txtBankName.Text); if (IsValidDescription == false) { errMessage += "Bank Name: Only Alphabets, Space, _ - allowed."; } 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 = System.Web.HttpUtility.HtmlEncode(errMessage); lblErrorMessage.Text = hfErrorMessage.Value; // This line should be changed lblErrorMessage.InnerHtml = errMessage; // Add this line ScriptManager.RegisterStartupScript(this, this.GetType(), "ShowModal", "$('#modal_form_horizontal').modal('show');", true); } } }Explanation
lblErrorMessage.Text = hfErrorMessage.Value;: This line sets the text of the label, which will display the HTML tags as plain text.lblErrorMessage.InnerHtml = errMessage;: This line sets the inner HTML of the label, which will render the HTML tags correctly as line breaks.By using
InnerHtml, the
tags will be rendered as line breaks in the error message, ensuring the message is displayed correctly within the modal.Thanks
Jignesh KumarRank #91 · 22.9K reputationPosted Aug 28, 2024, 1:26 AM EDT
Hello Ramco,
IS there any reason why are you using ? could you please try only
if (String.IsNullOrWhiteSpace(txtBankName.Text)) { errMessage += "Bank Name is required and cannot Be empty. "; } Boolean IsValidDescription = Common.CommonFunction.IsAlpha(txtBankName.Text); if (IsValidDescription == false) { errMessage += "Bank Name : Only Alphabets , Space , _ - allowed. "; }Ramco RamcoRank #462 · 5.4K reputationPosted Aug 28, 2024, 1:35 AM EDT
Hi Naimish
It is giving error message : Label does not contain a definition for InnerHtml
Thanks