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?
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?
this.Validators.Add(new CustomValidationError("Your message goes here."));
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
}
}