Two passes in custom validations

Last post 04-19-2008 4:47 PM by Archetti. 1 replies.

Sort Posts:

  • Two passes in custom validations

    04-17-2008, 5:45 PM
    • Loading...
    • Archetti
    • Joined on 04-17-2008, 4:37 PM
    • Posts 2

     Hi,

    I am writing a web application using Expression WEB and VWD2005.

    Because I start creating a web page with Expression Web, my pages don't have separate behind code pages. All the scripting is done in the .aspx page.

    I used few custom validation controls to determine if a certain value is already given to a field in a record already stored (say a customer sequence number, that is not the record key!).

    When debugging  the application with VWD2005 I noticed that the program passes twice trough the custom validation subroutines, which creates a problem to the application.

    In fact, when inserting new records, because I am checking if some value is already used, in the first pass (let's say that the value is not already used), the new record is inserted, but in a second pass, checking for the same value, it finds the newly added record, giving that way an error message stated in the custom validation control!

    I double checked this way of the program running twice trough the routines and even created a new very simple page with only a formviev and a custom validation control to be used during the insert process of a new record, but it actually runs that way. Is this normal or am I missing some point in the validation process?

    To give more details, I use to clear the command name property of the link buttons in the formView (the insert, update and delete buttons created automatically by the formview control) and instead use the OnClick event of the buttons to have more precise control over the insert, update or delete operations, using a try... catch structure. 

    Here is test example of some code for the update process. The code first executes the custom validation subroutines, than the update sub and then again the custom validation subroutines!

     thanks for helping

     

    1    <script runat=server>
    2
    3 Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
    4
    5 If args.Value > #12/31/2008# Then
    6 args.IsValid = False
    7 End If
    8 9 End Sub
    10
    11 Protected Sub
    CustomValidator2_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
    12
    13 If args.Value > 1000 Then 14 args.IsValid = False
    15 16 End If
    17 End Sub
    18 19 Protected Sub
    UpdateButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    20 If Page.IsValid Then
    21 Try
    22 23 FormView1.UpdateItem(True)
    24 Label1.Text = "Correzione inserita!" 25 Catch ex As Exception
    26 27 End Try
    28 End If
    29 30 End Sub
    31 32 </script>
    33
      

     

    Filed under:
  • Re: Two passes in custom validations

    04-19-2008, 4:47 PM
    Answer
    • Loading...
    • Archetti
    • Joined on 04-17-2008, 4:37 PM
    • Posts 2

     Problem solved .......but still don't understand why writing code the way I posted initially (that seems to me the simplest way!) the validation code must run one time before the onClick event handler and another time after the onClick event handler.

    Anyway, after dozens tries, I eventually came up to the following solution.

    First, I set the "CausesValidations" property to "False" to the button that postsback the page  (the UpdateButton in this case). This avoids running the validating codes as soon as the page loads.

    So the first code running is the UpdateButton_Click. Within this event handler I inserted a Page.Validate() call, that in turn calls the validation subroutines. Once the validation routines executed, the UpdateButton_Click event handler resumes testing the Page.isvalid property, deciding to execute or not the update command. At the end of the sub, program resumes showing the page with the result of the validation  process (validation summary messages) or the result of the command (Update in this case), so avoiding running once again the validation code for mysterious (to me!) reasons.
     The sample code below is different from the one posted initially, but nothing changes  related to the problem encountered.

    1        Protected Sub UpdateButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    2 Page.Validate()
    3 If Page.IsValid Then
    4 5 Try
    6 FormView1.UpdateItem(True)
    7 Catch ex As Exception
    8 9 End Try
    10 End If
    11 12 End Sub
    13 14 Protected Sub
    CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
    15 If args.Value = "President" Then 16 args.IsValid = False
    17 End If
    18 End Sub
    19 20 Protected Sub
    CustomValidator2_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
    21 If args.Value = "Ufficio" Then 22 args.IsValid = False
    23 End If
    24 End Sub
      
Page 1 of 1 (2 items)