views:

20

answers:

1

I want to build a custom validator control that inherits from BaseValidator. It will only be used on textboxes in my asp.net application. How can I get access to the textbox itself (read properties of the textbox) within the custom validator?

Here is what I have in my EvaluateIsValid function:

 Dim t As TextBox = CType(Page.FindControl(Me.ControlToValidate), TextBox)
 Return t.Text.Length <= t.MaxLength

It can't seem to find the control, so it breaks with a null reference exception. Can I do this another way?

Thanks!

+1  A: 

To get the textbox:

Dim t As TextBox = CType(Me.FindControl(Me.ControlToValidate), TextBox)

Paulus E Kurniawan
According to your code, t is a string, not a textbox
Dan Appleyard
I've updated the code.
Paulus E Kurniawan
Me.FindControl did it. I don't know why - I would think that b/c the textbox is not in the naming container of the validator it would not work, but it did. thanks!
Dan Appleyard
You're welcome, glad I could help.
Paulus E Kurniawan