6

I'm a huge fan of asp.net 4.5 webforms model binding using data annotations.

ascx:

     <asp:FormView ItemType="Contact" runat="server" DefaultMode="Edit" 
     SelectMethod="GetContact" UpdateMethod="SaveContact">
        <EditItemTemplate>   

              <asp:ValidationSummary runat="server" ID="valSum" />

              Firstname: <asp:TextBox  runat="server"  ID="txtFirstname" Text='<%#: BindItem.Firstname %>' /> 


              Lastname: <asp:TextBox  runat="server"  ID="txtLastname" Text='<%#: BindItem.Lastname %>' />

              Email:  <asp:TextBox  runat="server"  ID="txtEmail" Text='<%#: BindItem.Email %>' />     

              <asp:Button ID="Button1"  runat="server" Text="Save" CommandName="Update" />
        </EditItemTemplate>   
    </asp:FormView>

.cs:

    public void SaveContact(Contact viewModel)
    {
        if (!Page.ModelState.IsValid)
        {
            return;
        }            
    }              

    public Contact GetContact() 
    {
         return new Contact();
    }

Model:

    public class Contact
    {
        [Required]
        [StringLength(10, ErrorMessage="{1} tis te lang")]   
        public string Firstname { get; set; }

        [Required]
        [StringLength(10)]
        public string Lastname { get; set; }

        [Required]
        [EmailAddress]       
        public string Email { get; set; }

    }

Question:

Is client side validation supported out-of-the-box in webforms like in MVC? Or should we rely on third party libraries (DAValidation). Is it possible to port the goodness of Html.EnableClientValidation() to webforms ?

Regards,

Bart

BGR
  • 131
  • 1
  • 7
  • 1
    I guess you have to use Unobtrusive Validation see here : http://www.codeguru.com/csharp/.net/net_asp/using-unobstructive-validation-in-asp.net-4.5-web-forms.htm – Zaki Nov 12 '13 at 09:44
  • 1
    Hi Sam, For the moment there is no client validation at all (unobtrusive or not). The mentioned link is using requiredfieldvalidator's / regularexpression validators in ascx. I would like to avoid having to specify validation rules in 2 places (data annotations model + ascx). Client-side validation should be build right of the annotations specified on the model. Bart – BGR Nov 12 '13 at 10:38
  • Unfortunately no, client side validation for data annotation attributes is not available (but its available for server side with `DynamicValidator`). You can write your own validator or use one that I have wrote a while ago - http://davalidation.codeplex.com/. – Alexander Manekovskiy May 10 '15 at 17:54
  • Guys webforms have complete client side validation, it just needs to be specified once in the model, that's the whole point of Model Binding and DataAnnotations check my answer below – Sundara Prabu Sep 11 '17 at 07:37

2 Answers2

1

As we have found in our ASP.NET WebForms projects, there is no overall useful reuse of the Model's validation attributes for client side validation.

For example, a contact data model, with various properties like name, email, birthday etc... is not always used the same way. Sometimes it may have some mandatory fields, sometimes not, and even the required input data may differ at various points in the application.

Thus, in our projects, we use both a client side validation implementation, and the model attributes.

The general idea we apply is:

  • On the client side, we want to be as specific as possible, to avoid unnecessary postbacks and to provide the user with immediate, specific responses.
  • On the server side, we apply the model attributes plus more database and business-oriented validation rules and fail not so specifically. Plus, if required, some "inter-property" validation does take place when some fields are dependent from each other.

For the client side, we have chosen the jQuery Validate Plugin (http://jqueryvalidation.org/).

We even have built our own set of controls (that derive from the built-in WebControls), which render various (and even some custom) data rules.

Marcel
  • 15,039
  • 20
  • 92
  • 150
0

Guys webforms have complete client side validation, it just needs to be specified once in the model, that's the whole point of Model Binding and DataAnnotations check these links below. In short Add the following reference to your Model class library project. using System.ComponentModel.DataAnnotations;

Add this line to your web.config

<add key="ValidationSettings:UnobtrusiveValidationMode" value="WebForms"/>

Follow this link for details Webforms Model Validation

And this link for Microsoft's complete documentation Microsoft documentaion

Sundara Prabu
  • 2,361
  • 1
  • 21
  • 20