views:

813

answers:

3

Hi, is there any way to get the ASP.NET validation summary control to just display the HeaderText when there is a validation error?

Basically what I want is for just a simple message like "Please complete all fields marked *" to appear next to the submit button. As I'm using "*" for the error message on the validators I don't want these to appear in the summary.

Thanks for any help.

A: 

You could use a CustomValidator and set the ClientValidationFunction property to execute a JavaScript function that would populate a label to display your message.

ASPX:

 <asp:CustomValidator ID="validator" runat="server" ErrorMessage="*" ClientValidationFunction="Validate" ControlToValidate="controltovalidate" ValidateEmptyText="true"></asp:CustomValidator>

JavaScript:

function Validate(sender,args)
{
    args.IsValid = true;
    if(args.Value == "")
    {
        document.getElementById('YourCustomMessageLabel').innerText = "Please complete all fields marked *"
        args.IsValid = false;    
    }
}
Phaedrus
A: 

If you just use the Text property of your validator controls and leave the ErrorMessage property blank then that should solve your problem.

mdresser
+5  A: 

Set each validators Text to "*" and ErrorMessage to an empty string.

<form id="form2" runat="server">
Name:<br />
<asp:TextBox ID="NameTextBox" runat="server" />
<asp:RequiredFieldValidator 
    ID="NameTextBoxRequiredValidator" 
    ControlToValidate="NameTextBox"
    ErrorMessage="" 
    Text="*" 
    runat="server" />
<br />
City:<br />
<asp:TextBox ID="CityTextBox" runat="server" />
<asp:RequiredFieldValidator 
    ID="CityTextBoxRequiredValidator" 
    ControlToValidate="CityTextBox"
    ErrorMessage="" 
    Text="*" 
    runat="server" />
<br />
<asp:Button ID="SubmitButton" Text="Submit" runat="server" />
<hr />
<asp:ValidationSummary 
    ID="valSum" 
    DisplayMode="SingleParagraph" 
    HeaderText="Please complete all fields marked *"
    runat="server" />
</form>
Iain M Norman
bob on, cheers for that