Cannot figure out how to prevent a gridview from loading if a validator comes back with an error.

Last post 03-16-2007 5:15 AM by lernendotnet. 5 replies.

Sort Posts:

  • Cannot figure out how to prevent a gridview from loading if a validator comes back with an error.

    03-14-2007, 2:57 PM
    • Loading...
    • yulia77
    • Joined on 03-14-2007, 6:35 PM
    • Posts 3

    Does anyone know how can I prevent my gridview from loading if an invalid date was captured by a compare validator? 

    I have a form on a page, where, among other fields, a user can type in a date in a textfield. Based on that date, I have several grid views that load information for that user and date. The datasources for these views are SQL server stored procedures with parameters. I placed these gridview on the page with the user of the wizard.

    Everything works, but I'm working on the validation part, where a user might type in an invalid date format. So, I have a compare validator on the page to check the validity of the format of the date field. After I type in an invalid date, if I press enter then the validator fires off as it supposed to and doesn't load the grid views, but if I tab out of the field, the page fires off the validation, but it continues on with loading of the grid view and I get this error: "

    String was not recognized as a valid DateTime.

     

     I tried to use this in my VB code page:

    Protected Sub dsHours_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles dsHours.DataBinding

    If CompletionDateCompareValidator.IsValid Then

    GridViewHours.DataBind()

    End If

    End Sub

    Here is my compare validator code:

    <

    asp:CompareValidator id="CompletionDateCompareValidator"

    runat="server" Display="Dynamic"

    ErrorMessage="Date format is incorrect."

    ControlToValidate="txtDate"

    Operator="DataTypeCheck" Type="Date" Width="145px" EnableClientScript="true"></asp:CompareValidator>
  • Re: Cannot figure out how to prevent a gridview from loading if a validator comes back with an error.

    03-15-2007, 5:30 AM
    Answer
    • Loading...
    • addie
    • Joined on 03-02-2007, 11:12 AM
    • Posts 291

    I am not sure whether you should be using a CompareValidator in this case. If you are just trying to validate whether a correct date is entered in a textbox, then associate a RegularExpressionValidator with it and use a regular expression like below

    ^((0[1-9]|1[0-2]))-((0[1-9]|[12][0-9]|3[0-1]))-(([0-9][0-9][0-9][0-9]))$ - for a date format mm-dd-yyyy. That will work i hope.

  • Re: Cannot figure out how to prevent a gridview from loading if a validator comes back with an error.

    03-15-2007, 6:49 AM
    i think you have to check for valid date on page submit event also
    --------------------------------------------
    Dont Forget to mark the reply as ANSWER if you could solve the condition successfully.
  • Re: Cannot figure out how to prevent a gridview from loading if a validator comes back with an error.

    03-15-2007, 10:01 AM
    • Loading...
    • yulia77
    • Joined on 03-14-2007, 6:35 PM
    • Posts 3

    Addie,

     Thanks for your reply. I did try using the regular expression as well, but I get the exact same result.

    My gridview loads based on the two parameters from the form. As soon as the form loads, information is filled in the text boxes, coming from my database. Based on the info in the textboxes, the gridview loads right away without any submits. Then if a user changes the date on the form, the gridview gets automatically reloaded since the info in the textbox has changed.

    Here is the problem - when I change a date in the textbox and hit Enter on the keyboard, the validation activates and prevents the gridview from loading (which is right). However, as soon as I click out or tab out of the textbox, the gridview attempts to load, but then I get the page error.

    What I need is a function in the code-behind so that the dataset for the gridview's datasource does not load if the validation has not passed, like this:

    Protected Sub dsHours_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles dsHours.DataBinding

    If CompletionDateCompareValidator.IsValid Then

    GridViewHours.DataBind()

    End If

    End Sub

     However, this function doesn't seem to help, but I think I'm on the right track though. Any thoughts?

     

  • Re: Cannot figure out how to prevent a gridview from loading if a validator comes back with an error.

    03-15-2007, 10:06 AM
    • Loading...
    • yulia77
    • Joined on 03-14-2007, 6:35 PM
    • Posts 3

    Thanks for you reply!

    I did try checking for valid date on the page submit as well. Is this the correct way of checking it in the submit function?

    If CompletionDateCompareValidator.IsValid = True Then

    GridViewHours.DataBind()

    End If
  • Re: Cannot figure out how to prevent a gridview from loading if a validator comes back with an error.

    03-16-2007, 5:15 AM
    Answer

    there is one more way for validating page while submit.

    it is as follows:

    in html design page-->

    <form id="form1" onsubmit="validate()">

    you can write validate() function in javascript also

    --------------------------------------------
    Dont Forget to mark the reply as ANSWER if you could solve the condition successfully.
Page 1 of 1 (6 items)
Microsoft Communities
Page view counter