dynamic generation of a CustomValidator

  • Thread starter Thread starter Markus Broy
  • Start date Start date
M

Markus Broy

Hello everybody,

I have a problem concerning the dynamic creation of a CustomValidator.

If I create a CustomControl as shown in the code below and the page is post
back the validation procedure is never called.

In Page_Load Page Validate() is called and in the control which should be
validated there is a non numeric value.

Why is the procedure "checkProd" never calles? What's wrong?

Here the code extract:

cell = New HtmlTableCell
txt = New HtmlInputText
txt.ID = "txt1"

val = New CustomValidator
val.EnableClientScript = False
val.Text = "*"
val.ErrorMessage = "Eingabe ungültig"
val.ControlToValidate = "txt1"
AddHandler val.ServerValidate, AddressOf checkProd

cell.Controls.Add(val)

...

Protected Sub checkProd(ByVal source As Object, ByVal args As
ServerValidateEventArgs)
If Not IsNumeric(args.Value) Then
args.IsValid = False
Else
args.IsValid = True
End If
End Sub

Thank you and best regards

Markus
 
Maybe try adding your control during the MyBase/Page.Init event. Also
make sure you're only adding it when Page.IsPostback = False
 
Well this is kind of a hack and not as efficient as possible, but I've
had to do it before.
Add all the controls you think you'll need to the page in a static
fashion with enabled and visible set to false, then set visibile and
enabled to true when you need to in the code behind. That should do
trick. You should be able to dynamically control the text and
errormessage for these, but they'll be on the page when it first loads.
 
ajamrozek said:
Maybe try adding your control during the MyBase/Page.Init event. Also
make sure you're only adding it when Page.IsPostback = False

Actually, dynamic controls should be added to the page *every* time. If you
don't recreate the control on the postback there's nothing to fire the events.
I like to create my dynamic controls as early in processing as is
reasonable; I recommend either doing it from the Init or by overriding
CreateChildControls (since that seems appropriately named if nothing else!).
 
Actually, dynamic controls should be added to the page *every* time. If
you
don't recreate the control on the postback there's nothing to fire the
events.

That was the point. Now it works ;-)

But recreating is not comfortable :-(

Thank you all
 
Back
Top