Compare validation in gridview

Last post 02-19-2008 4:27 AM by Jon Kemp. 7 replies.

Sort Posts:

  • Compare validation in gridview

    06-09-2007, 6:00 PM
    • Member
      25 point Member
    • deagle25
    • Member since 07-18-2005, 7:09 AM
    • Posts 8

    I have a gridview in the content area on a masterpage. There is a checkbox and a textbox that I would like to compare and validate on update. If the checkbox is not checked the textbox must not be empty (required field).

    What I have done so far is to convert the two fields into Templatefields and added a required field validator to validate the textbox inside its EditItemTemplate - so far so good. Now if I could just disable the requiredfieldvalidator if the checkbox is not checked that would do the trick, but I can't seem to make it work.

     Can anyone help me?

  • Re: Compare validation in gridview

    06-10-2007, 12:16 AM
    • Contributor
      4,008 point Contributor
    • Matt-dot-net
    • Member since 06-09-2007, 4:00 PM
    • Kentucky
    • Posts 772

    In the EditItemTemplate containing the TextBox, replace the RequiredFieldValidator with a CustomValidator.  Make sure you set the property ValidateEmptyText="true". Subscribe to the OnServerValidate event and use some code that looks like this:

     

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            CustomValidator cv = (CustomValidator)source;
            GridViewRow row = (GridViewRow)cv.NamingContainer;
            //assuming the id of the CheckBox control in the EditItemTemplate is "CheckBox1"
            CheckBox chkBox = (CheckBox)row.FindControl("CheckBox1");
    
            args.IsValid = true;
            if (!chkBox.Checked)
            {
                if (string.IsNullOrEmpty(args.Value))
                    args.IsValid = false;
            }
        }

    I will leave the ClientValidationFunction implementation up to you! Smile

    Cheers!

    Filed under:
  • Re: Compare validation in gridview

    06-10-2007, 5:49 PM
    • Member
      25 point Member
    • deagle25
    • Member since 07-18-2005, 7:09 AM
    • Posts 8

    Thank you Matt! It certainly gave me some clues. Could you translate it to VB?

  • Re: Compare validation in gridview

    06-10-2007, 6:39 PM
    Answer
    • Contributor
      4,008 point Contributor
    • Matt-dot-net
    • Member since 06-09-2007, 4:00 PM
    • Kentucky
    • Posts 772

     

        Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
            Dim cv As CustomValidator = source
            Dim row As GridViewRow = cv.NamingContainer
    
            'assuming the id of the CheckBox control in the EditItemTemplate is "CheckBox1"
            Dim chkBox As CheckBox = row.FindControl("CheckBox1")
    
            args.IsValid = True
    
            If (Not chkBox.Checked) Then
                If (String.IsNullOrEmpty(args.Value)) Then
                    args.IsValid = False
                End If
            End If
        End Sub
     
    Filed under:
  • Re: Compare validation in gridview

    07-01-2007, 3:29 AM
    • Member
      25 point Member
    • deagle25
    • Member since 07-18-2005, 7:09 AM
    • Posts 8

    Thank you, Matt! It worked perfectly Cool

  • Re: Compare validation in gridview

    08-03-2007, 12:05 PM
    • Member
      12 point Member
    • Jon Kemp
    • Member since 08-03-2007, 11:53 AM
    • Posts 27

    Hi Matt,

    I am a newbie and followed your c# code for a detailsview (for inserting) rather than a gridview object. Only slight difference is I need to to validate if the checkbox is selected and the textbox is empty.

    I made the adjustment (think they are correct, please tell me if wrong) I have been debugging the code and it is working, (I.e the code identifies that the texbox is empty, marks the args.isvalid =false) but it does not stop the inserting of the record.  It is really annoying me now.  Do you have any suggestions, the setup has been configured the same as above (validate empty text = true) . I notice that in the other posts you mentioned "ClientValidationFunction", I just want it to fire "please enter number" the same way as the required field validator works (which requires no client side functions) , I suspect thats where I am getting lost maybe?.  I would appreciate any insight you may have. Thanks in advance. Heres my code....

     

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            CustomValidator cv = (CustomValidator)source;
            DetailsView myval = (DetailsView)cv.NamingContainer;        
            //GridViewRow row = (GridViewRow)cv.NamingContainer;
            //assuming the id of the CheckBox control in the EditItemTemplate is "CheckBox1"
            CheckBox chkBox = (CheckBox)myval.FindControl("cbLPGACertYN");
    
            args.IsValid = true;
            if (chkBox.Checked)
            {
                if (string.IsNullOrEmpty(args.Value))
                 args.IsValid = false; 
            }
    
        }
    
     
    Thanks,

    JK
  • Re: Compare validation in gridview

    02-18-2008, 1:18 PM
    • Contributor
      4,008 point Contributor
    • Matt-dot-net
    • Member since 06-09-2007, 4:00 PM
    • Kentucky
    • Posts 772

    Can you show your insert code?  Are you making sure to query the Page.IsValid property before performing insert?

  • Re: Compare validation in gridview

    02-19-2008, 4:27 AM
    • Member
      12 point Member
    • Jon Kemp
    • Member since 08-03-2007, 11:53 AM
    • Posts 27

    Hi Matt,

     I posted this in August and to be honest, I cannot recall how I fixed it.  But this is no longer an issue.  Thanks for getting back in touch Smile

     Regards,

     JK

    Thanks,

    JK
Page 1 of 1 (8 items)