Double server validate with CustomValidator in RadGrid

6 posts, 1 answers
  1. Louis
    Louis avatar
    7 posts
    Member since:
    Oct 2015

    Posted 30 Oct 2015 Link to this post

    Why when I'm using asp.net CustomValidator inside Insert/Edit ItemTemplate column of my RadGrid, the ServerValidate method is fired two times ?

    I'm using telerik version 2015.3.930.45

    Example:

    Asp.net

     

    <telerik:GridTemplateColumn UniqueName="ProductName" HeaderText="Product">
       <ItemTemplate><%# Eval("ProductName") %></ItemTemplate>
       <EditItemTemplate>
          <asp:TextBox Id="ProductNameText" Text='<%# Eval("ProductName") %>' runat="server"/>
          <asp:CustomValidator ID="ProductNameValidator" ControlToValidate="ProductNameText" Text="*" ErrorMessage="*" OnServerValidate="ProductNameValidator_OnServerValidate" runat="server"/>
       </EditItemTemplate>
    </telerik:GridTemplateColumn>

     

    C#

    protected void ProductNameValidator_OnServerValidate(object source, ServerValidateEventArgs args)
    {
       var newProductName = args.Value;
       args.IsValid = _myProducts.All(x => x.ProductName != newProductName);
    }
     ​

  2. Louis
    Louis avatar
    7 posts
    Member since:
    Oct 2015

    Posted 30 Oct 2015 Link to this post

    Sorry for the confusion about my first sentence.

    My question is, why "OnServerValidate" method is fired two times, when customValidator are inside RadGrid ?

  3. Maria Ilieva
    Admin
    Maria Ilieva avatar
    4017 posts

    Posted 03 Nov 2015 Link to this post

    Hello Louis,

    I revise the presented column declaration and it looks absolutely correct without anything obvious that may lead to the described behavior. You can revise the online demo below where the OnServerValidate fires correctly and see what the difference in your case is:
    http://demos.telerik.com/aspnet-ajax/grid/examples/data-editing/validation/defaultcs.aspx


    Regards,
    Maria Ilieva
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  4. Louis
    Louis avatar
    7 posts
    Member since:
    Oct 2015

    Posted 03 Nov 2015 Link to this post

    After a review of your exemple, it's also fired CustomValidation twice after user click on the "Insert"/"Update" button.

     It's seem to be a asp.net webform bugs with CustomValidator include inside a Templated List control.

     The way we correct this "double validation" is to override Page.Validate()

    private bool _wasValidated;
    protected override Validate() {
       if(!_wasValidated)
          base.Validate();
    }

    Thanks for you quick response to my thread

  5. Louis
    Louis avatar
    7 posts
    Member since:
    Oct 2015

    Posted 03 Nov 2015 Link to this post

    in fact...

     

    private bool _wasValidated;
    protected override Validate() {
       if(!_wasValidated) {
          base.Validate();
          _wasValidated - true;
       }
    }

  6. Answer
    Maria Ilieva
    Admin
    Maria Ilieva avatar
    4017 posts

    Posted 04 Nov 2015 Link to this post

    Hi,

    Thank you for sharing your findings. I'm sure this will help other users that are facing the same behavior.

    Regards,
    Maria Ilieva
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top