Results 1 to 8 of 8

Thread: Dynamically generated CompareValidator not working right.

  1. #1

    Thread Starter
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Ancient City, U.S.
    Posts
    1,254

    Question Dynamically generated CompareValidator not working right.


    I'm new to this asp.net stuff. My dev lead and I are both trying to learn how to do this. He built some pages and asked me to add the validation controls to them.

    The first page has a series of text boxes with autopostback = true. I can put CompareValidators onto the form and assign them to the textboxes, and they seem to work right.

    On my second page, however, the controls are being built on the server using vb code. The CompareValidators seem to work on the client side. On the server side, however, the CompareValidators always have an IsValid property = true, even when the value of the textbox clearly violates the conditions. I've checked all the properties I can think of and I can't see any difference between these dynamically generated validators and the static ones I have on the first page, so I'm thinking there must be something else going on.

    Let me know if you need any more info or have any ideas.
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  2. #2

    Thread Starter
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Ancient City, U.S.
    Posts
    1,254

    Re: Dynamically generated CompareValidator not working right.

    Well, I never figured out why they are showing valid on the server side, but I did discover that when I set textbox.causesValidation = true, the client side validation occurs and the value never even goes to the server. So basically my problem is solved - no more invalid values getting to the server.

    I feel like such a noob.
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  3. #3
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Dynamically generated CompareValidator not working right.

    Hello,

    Is it possible to show the code that you are using? It sounds like the controls that you are creating don't have their event handlers hooked up at the right point, therefore when the events fire, they are not being called.

    The reason that I ask is that the problem isn't really solved. You can't rely on JavaScript being enabled on the page, or that some malicious user won't manually switch off JavaScript. As a result, you should definitely still validate the input on the server side. Client side validation should be seen as a convenience for the user preventing the need for round trips to the server, not as the only validation.

    Thanks

    Gary

  4. #4

    Thread Starter
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Ancient City, U.S.
    Posts
    1,254

    Re: Dynamically generated CompareValidator not working right.

    OK. Here's the snippet of code that creates the panel, textbox, and validator:
    Code:
                        'add a spot to hold the value
                        Dim ValueCell As New Web.UI.HtmlControls.HtmlTableCell
                        Dim Panel As New UpdatePanel
                        Panel.UpdateMode = UpdatePanelUpdateMode.Conditional
                        Panel.ID = "panel_" & Variable.Name
                        ValueCell.Controls.Add(Panel)
                        NewRow.Cells.Add(ValueCell)
    
                        'add the editor for this item
                        Dim EditorControl As Control = Editor.CreateVariableEditor(Variable, HostTable.Page, VariableManager)
                        Panel.ContentTemplateContainer.Controls.Add(EditorControl)
    
                        'add the validation control - if any.
                        Dim ValidationControl As CompareValidator
                        ValidationControl = Editor.CreateVariableValidator(Variable, HostTable.Page, VariableManager)
                        If ValidationControl IsNot Nothing Then
                            ValidationControl.ControlToValidate = EditorControl.ID
                            Panel.ContentTemplateContainer.Controls.Add(ValidationControl)
                        End If
    Here's the snippet from CreateVariableEditor that creates the textbox:
    Code:
                        Dim TextBox As New TextBox
                        TextBox.AutoPostBack = True
                        TextBox.CausesValidation = True
                        TextBox.ID = "VAR_" & Variable.Name
                        TextBox.Text = Page.Session.Item(Variable.Name)
                        AddHandler TextBox.TextChanged, AddressOf Editor.VariableTextBox_TextChanged
                        CreateVariableEditor = TextBox
    and here's the snippet from CreateVariableValidator that creates the CompareValidator:
    Code:
                    Dim ValBox As New CompareValidator()
                    With ValBox
                        .ValueToCompare = 10
                        .Type = ValidationDataType.Integer
                        .Operator = ValidationCompareOperator.GreaterThan
                        .ErrorMessage = "Value must be greater than " & .ValueToCompare.ToString
                        .ID = Variable.Name.ToUpper & "_VALIDATOR"
                        .SetFocusOnError = True
                    End With
    Finally, the event handler and a related function:
    Code:
        Public Shared Sub VariableTextBox_TextChanged(sender As Object, e As System.EventArgs)
    
            With CType(sender, TextBox)
    
                'get the variable name
                Dim VarName As String = .ID.Substring(4)
    
                'fish out any validator and check it
                Dim Validator As CompareValidator = GetValidator(CType(sender, TextBox), VarName)
    
                'apply the variable value
                Using VariableMgr As New SessionVariables(.Page.Session)
                    If Validator IsNot Nothing Then
                        If Validator.IsValid Then
                            VariableMgr.SetSessionVariable(VarName, .Text)
                        End If
                    Else
                        VariableMgr.SetSessionVariable(VarName, .Text)
                    End If
    
                End Using
    
            End With
    
        End Sub
    
        Private Shared Function GetValidator(ByVal C As Control, Name As String) As Control
    
            For Each ValC As Control In C.Parent.Controls
                If ValC.ID.Contains(Name & "_VALIDATOR") Then
                    Return ValC
                End If
            Next
    
        End Function
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  5. #5
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Dynamically generated CompareValidator not working right.

    Hey,

    Which page level events are you doing this work in though?

    Is it possible to upload the entire aspx and aspx.vb files?

    If not, can you put it together in a small sample application and upload that?

    Ultimately, I suspect the issue is going to be with when the control are created within the ASP.Net Page Life Cycle, as described here:

    http://www.4guysfromrolla.com/articles/092904-1.aspx

    Gary

  6. #6

    Thread Starter
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Ancient City, U.S.
    Posts
    1,254

    Re: Dynamically generated CompareValidator not working right.

    It's happening through the page_load event. I'll try to upload the page and support class tomorrow.
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  7. #7
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Dynamically generated CompareValidator not working right.

    Quote Originally Posted by dolot View Post
    It's happening through the page_load event. I'll try to upload the page and support class tomorrow.
    Ok, so as per the article that I linked to, that is not early enough in the ASP.Net Page Life Cycle. You will have to alter your code as per the article.

    Gary

  8. #8

    Thread Starter
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Ancient City, U.S.
    Posts
    1,254

    Re: Dynamically generated CompareValidator not working right.

    Thanks, Gary. I'll go ahead and read that article and the companion articles with it and go from there. If I'm still having issues I'll post again.
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width