views:

3304

answers:

4

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?

+1  A: 

To add error message on validation summary you can use EnableClientScript property of ValidationSummary and the other validation controls. Set EnableClientScript to false all of them :

<asp:ValidationSummary
HeaderText="You must enter a value in the following fields :"
DisplayMode="BulletList"
EnableClientScript="false"
runat="server"/>

For highlighting a control, no it's not possible with current controls.

But I put my validation controls near the related controls, and I set their Text property as "*". Then if the validation fails, it appears near failed control.

Maybe you can use custom validator to highlight the failed control. But you should write your own implementation.

Canavar
I want to add error messages upon postack via codebehind.
Blankman
+8  A: 

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;
}
One trap here is that if your validation summary control specifies a ValidationGroup, then you also need to add a ValidationGroup attribute to your CustomValidator to ensure the error message is displayed when/where required. Maybe just commonsense but it's caught me out occasionally.
David Clarke
+1  A: 

Try this control http://www.codeproject.com/KB/validation/validationsummary.aspx

A: 

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.

NightOwl888