This is a migrated thread and some comments may be shown as answers.

Custom Validator fires, but does not stop update

5 Answers 417 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jeff Mikla
Top achievements
Rank 1
Jeff Mikla asked on 25 Jun 2009, 04:35 PM
Hi,

I have a required field validator and a custom validator tied to an asp:textbox within a RadGrid.  The required field validator fires and displays my error message when the textbox is empty.  The custom validator fires its OnServerValidate call, my code checks the text length and sets the args.IsValid = false if the text is too large.

But the Update continues to proceed and tries to update the database with some text that is too large for the database field.
The custom validator error message never displays.

What am I missing?

 

<EditItemTemplate>

 

 

 

    <asp:TextBox ID="txbDescription" Style="width: 390px; margin-left: 4px;" runat="server"

 

 

        TextMode="MultiLine" Rows="3" Text='<%# Bind("Description") %>' MaxLength="1000" Wrap="true" />

 

 

 

    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txbDescription"

 

 

        ErrorMessage="A Document description is required" Enabled="true" runat="server">

 

 

    </asp:RequiredFieldValidator>

 

 

    <asp:CustomValidator ID="val_txbDescription" Enabled="true" runat="server"

 

 

        ErrorMessage="Description is too large. Please limit text to 1000 characters."

 

 

        OnServerValidate="val_txbDescription_ServerValidate" Display="Dynamic" ControlToValidate="txbDescription">

 

 

    </asp:CustomValidator>

 

 

 

</EditItemTemplate>

 

 

 

 

protected

 

void val_txbDescription_ServerValidate(object source, ServerValidateEventArgs args)

 

{

args.IsValid =

true;

 

 

if (args.Value.Length > 1000) args.IsValid = false;

 

}

 

5 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 26 Jun 2009, 09:48 AM
Hi,

You need to cancel the update process based on whether the page is valid or not  after performing the custom validation as shown below:

private void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
{
  
if(e.CommandName == RadGrid.UpdateCommandName)
  {
   
if(!Page.IsValid)
   {
    e.Canceled = true;
   }
  }
}


Could you make sure that you have done the same.

Thanks,
Princy

0
Jeff Mikla
Top achievements
Rank 1
answered on 02 Jul 2009, 06:15 PM
Thanks, that did the trick.
0
Stacy
Top achievements
Rank 1
answered on 29 May 2012, 01:41 PM
Ignore my message, I was using a formview
0
Gary
Top achievements
Rank 1
answered on 09 Apr 2014, 08:02 PM
Princy,
I just came across this post as I was having the same issue. When I add the check for Page.IsValid(), it does stop the update, but when the page posts back, the validator error messages are no longer displayed. 

What am I doing wrong here?
0
Princy
Top achievements
Rank 2
answered on 10 Apr 2014, 09:59 AM
Hi Gary,

I was not able to replicate such an issue at my end. Please try to replicate it in this demo on Flexible Server-Side Validation.
If this doesn't help, please provide your code snippet.

Thanks,
Princy
Tags
Grid
Asked by
Jeff Mikla
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Jeff Mikla
Top achievements
Rank 1
Stacy
Top achievements
Rank 1
Gary
Top achievements
Rank 1
Share this question
or