views:

73

answers:

2

When a user tries to save a piece of content, if it had any issues I want to insert a message into my asp.net validation summary control.

how can I do this?

+1  A: 
this.Validators.Add(new CustomValidationError("Your message goes here."));
David Stratton
A: 

Add a CustomValidator to your form:

<asp:CustomValidator ID="myValidator" runat="server" Display="None" OnServerValidate="myValidator_ServerValidate" />

In your code-behind page, you'd then define myValidator_ServerValidate:

protected void myValidator_ServerValidate(object source, ServerValidateEventArgs args)
{
    args.IsValid = true;

    if (txtName.Text.Trim().Length < 1)
    {
        args.IsValid = false;
        myValidator.ErrorMessage = "Enter your name";
        return;
    }
    // ...
}

And your save button would simply check if the page is valid:

protected void btnAppoint_Click(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        // code to save user information
    }
}
Druid