Unfortunately the WebForms model binding validation framework is not as comprehensive as MVC yet. For one, client side validation of data annotations is not built in. And the only way that I know of to display data annotation errors is via the ValidationSummary control, which has a ShowModelStateErrors property (default is true). Here is a super simple example:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:FormView ID="FormView" runat="server" RenderOuterTable="false" ItemType="Person"
DefaultMode="Insert" InsertMethod="FormView_InsertItem">
<InsertItemTemplate>
<asp:ValidationSummary ID="ValSummary" runat="server" ValidationGroup="FormGroup"
HeaderText="The following problems occured when submitting the form:" />
<table>
<tr>
<td>
<asp:Label ID="NameLabel" runat="server" AssociatedControlID="NameField">Name</asp:Label>
</td>
<td>
<asp:TextBox ID="NameField" runat="server" Text='<%# BindItem.Name %>' ValidationGroup="FormGroup" />
</td>
</tr>
</table>
<asp:Button ID="SaveButton" runat="server" CommandName="Insert" Text="Save" ValidationGroup="FormGroup" />
</InsertItemTemplate>
</asp:FormView>
</form>
</body>
</html>
Code Behind:
public partial class create_person : System.Web.UI.Page
{
public void FormView_InsertItem()
{
var p = new Person();
TryUpdateModel(p);
if (ModelState.IsValid)
{
Response.Write("Name: " + p.Name + "<hr />");
}
}
}
Class:
using System.ComponentModel.DataAnnotations;
public class Person
{
[Required, StringLength(50)]
public string Name { get; set; }
}
Because there is no client side / javascript support for data annotation validation, the only way to generate model state errors is after a postback. If you need inline validation next to your controls, you still need to use the standard validation controls.
I have posted a feature request on user voice asking them to consider improving the data annotation integration:
http://aspnet.uservoice.com/forums/41202-asp-net-web-forms/suggestions/3534773-include-client-side-validation-when-using-data-ann
If you like the idea please vote it up on user voice. Hope this helps.