dynamic generation of a CustomValidator

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
 
A

ajamrozek

Maybe try adding your control during the MyBase/Page.Init event. Also
make sure you're only adding it when Page.IsPostback = False
 
A

ajamrozek

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.
 
G

Guest

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!).
 
M

Markus Broy

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top