Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On postback, how can I add a error message to validation summary?

Two questions:

On postback when a user clicks submit, how can I add a error message to validation summary?

Is it also possible to highlight a particular textbox using the built in .net validation controls?

like image 446
Blankman Avatar asked Apr 22 '09 15:04

Blankman


People also ask

How do you show custom validator error message in validation summary?

In order for the Validator 's error message to display in the ValidationSummary , you need to set the Validator s Display="none" .

How do I create a custom error message in ValidationSummary?

AddModelError() method. The ValidationSummary() method will automatically display all the error messages added in the ModelState . Thus, you can use the ValidationSummary helper method to display error messages.

Which of the following validation control collects all the validation control error messages?

The Button control and validation control has a property called Validation Group in which we specify the group name. This control is mostly less used.It is used to collect all the validation control error messages and display it collectively on the screen.


2 Answers

Dynamically create a CustomValidator control and add it directly to the Page.Validators collection.

Dim err As New CustomValidator err.ValidationGroup = "MyGroup" err.IsValid = False err.ErrorMessage = "The password is invalid" Page.Validators.Add(err) 

Unlike adding the CustomValidator to the markup, this method allows you to add any number of arbitrary error messages based on server-side business logic.

Note that you can also add it to the page directly, but there are a couple of rules to follow:

  1. You must add the control to the same naming container as the controls of the validation group.
  2. If you don't want the validation message to appear in a random position in the page, you will either have to add the validator to a specific container or you will need to supress it using a CSS class or style.

You can also create a custom class and implement IValidator, which enables you to add the message with one line of code, but this method doesn't support Validation Groups.

Per Anders Fjeldstad's suggestion, here are a set of handy extension methods.

Imports Microsoft.VisualBasic Imports System.Runtime.CompilerServices  Public Module PageExtensions      <Extension()> _     Public Sub AddValidationError(ByVal p As System.Web.UI.Page, ByVal errorMessage As String)         p.AddValidationError(errorMessage, String.Empty)     End Sub      <Extension()> _     Public Sub AddValidationError(ByVal p As System.Web.UI.Page, ByVal errorMessage As String, ByVal validationGroup As String)         Dim err As New CustomValidator         err.ValidationGroup = validationGroup         err.ErrorMessage = errorMessage         err.IsValid = False         p.Validators.Add(err)     End Sub  End Module 
like image 61
NightOwl888 Avatar answered Sep 19 '22 03:09

NightOwl888


Add a custom validator and manually set it's IsValid and ErrorMessage properties. Sort of like this:

<asp:panel ID="ErrorsPanel" runat="server" CssClass="ErrorSummary">     <asp:CustomValidator id="CustomValidator1" runat="server"          Display="None" EnableClientScript="False"></asp:CustomValidator>     <asp:ValidationSummary id="ErrorSummary" runat="server"          HeaderText="Errors occurred:"></asp:ValidationSummary> </asp:panel> 

In the code behind:

// // Update the database with the changes // string ErrorDetails; if (!Db.Update(out ErrorDetails)) {     CustomValidator1.IsValid = false;     CustomValidator1.ErrorMessage = ErrorDetails; } 
like image 31
user53794 Avatar answered Sep 23 '22 03:09

user53794