views:

277

answers:

1

This should be a simple problem to fix, as it uses the same way I fixed my last problem with FooControl (below).

Basically, I want to add a derived validator I made to this composite control. It works fine but on postback it just disappears in the markup, making me think it's lost its ViewState.

I am probably doing something wrong with instantiating it, but I've tried setting only the ControlToValidate, moving things around, and nothing works.

I've provided some surrounding code to see what's working and then what's not.

    Private FooControl As IFooControl
    Private Validator As MyValidator

    Protected Overrides Sub CreateChildControls()

        FooControl = FooControlProvider.CreateFooControl(blah)

        Me.Controls.Add(FooControl.RetrieveControl())            

        ' Begin Not Working

        Validator = New MyValidator()
        Me.Controls.Add(Validator)

        Validator.ID = "MyValidatorID"
        Validator.ControlToValidate = FooControl.ID
        Validator.IsRequired = True ' Custom property
        Validator.ErrorMessage = "Please select an answer"

        ' End Not Working

        If Not DataSource Is Nothing Then
            FooControlProvider.AssignDataSource(DataSource, FooControl)
        End If
    End Sub
+1  A: 

I've found the problem. Apparently, on PostBack, no matter how you order the creation of the Validator, its display is set to None. I found this by breaking on the Render method and checking the Validator variable.

The solution (hack?) is to set the validator to your desired Display during the Render method.

    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        Validator.Display = ValidatorDisplay.Dynamic
        MyBase.Render(writer)
    End Sub
subkamran