Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I wanted to make a nice validation system in VB.NET and ASP.NET, where developers only need to specify a single line to validate their controls. I came up with a system, using lambda expressions. So basically, users would add their controls like this:

ValidationHandler.CurrentInstance.AddValidationRule(Function() txtFirstName.Text.Length > 0, txtFirstName)

This works perfectly with Windows Form Applications. I tried it on ASP.NET and the issue I have, is that on the second time I validate, it still holds the previous values. So for example, I have a textbox that has 0 length when it loads, it validates invalid. But, if I change it's length and click submit again, it still sees the textbox's length as 0.

enter image description here

enter image description here

And this is the problem.

Also, this is the code for ValidationHandler:

Imports System.Text

Public Class ValidationHandler

    'This are the rules.
    Private ValidationRules As New Dictionary(Of Func(Of Boolean), Control)

    'This is the error string builder. This will be empty if there are no errors.
    Public ErrorMessage As String    

    'This allows us to access the class without having to instantiate it. (Singleton pattren).
    Public Shared Property CurrentInstance As New ValidationHandler 

    ''' <summary>
    ''' Adds a validation rule.
    ''' </summary>
    ''' <param name="Rule">
    ''' The rule. 
    ''' </param>
    ''' <param name="control">
    ''' The control that validation is being applied on.
    ''' </param>
    ''' <remarks></remarks>
    Public Sub AddValidationRule(ByRef Rule As Func(Of Boolean), Control As Control)    
        'Adds that rule to the dictionary.
        ValidationRules.Add(Rule, Control)    
    End Sub    

    ''' <summary>
    ''' This function does all validation on the controls. 
    ''' </summary>
    ''' <returns>True if there are no errors and false if there are.</returns>
    ''' <remarks></remarks>
    Public Function IsValid() As Boolean

        'Clears the error message string builder.
        ErrorMessage = ""

        'Validates each rule against the control.
        For Each rule In ValidationRules

            If rule.Key.Invoke Then    
                Dim str = "yay"    
            Else    
                'Gets the error message from the tag, if the tag is not empty. 
                ErrorMessage = ErrorMessage & ("- " & rule.Value.ID & " is invalid." & "</br>" & Environment.NewLine)    
            End If    
        Next

        'Checks if there are any errors. If there are, return false, else return true.
        If ErrorMessage.Length > 0 Then
            Return False
        Else
            Return True
        End If

    End Function

    'Makes the class unable to be instantiated.
    Private Sub New()       

    End Sub

    Public Sub Clean()    
        ValidationRules.Clear()    
    End Sub    
End Class
share|improve this question
    
When did you call the ValidationHandler.CurrentInstance.IsValid() to validate inputs? –  ajakblackgoat May 25 '13 at 3:42
    
I call it on a button click. (It is basically a normal asp.net button that, upon click, validates the form) –  user2417731 May 26 '13 at 4:08
    
Can someone help please? I need this for a project that is pretty urgent. –  user2417731 May 28 '13 at 2:40

1 Answer 1

Can you see if your control in the validation is the exact same control on the page after a postback. I only asked because controls are recreated from scratch on a postback in web forms. You may have to pass in the control ID and search for it on the current page if thats the case (or something similar).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.