, WebForms , MVC. -, . , , , - ValidationSummary, ShowModelStateErrors ( ). :
<!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>
:
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 />");
}
}
}
:
using System.ComponentModel.DataAnnotations;
public class Person
{
[Required, StringLength(50)]
public string Name { get; set; }
}
/javascript , - . , .
, :
http://aspnet.uservoice.com/forums/41202-asp-net-web-forms/suggestions/3534773-include-client-side-validation-when-using-data-ann
, . , .