Search code examples
c#asp.netservercontrols

Attach RequiredValidator on custom server control rendering a TextBox


I don't know whether this is really possible, but I'm trying my best.

If I have a (complex) custom server control which (beside other controls) renders a TextBox on the UI. When placing the server control on a page, would it be possible to attach a RequiredField validator to that server control, such that the validator validates the Text property of that control which points to the Text property of the rendered TextBox?

Of course I could incorporate the RequiredField validator directly into the server control, but this is for other reasons not possible (we are rendering RequiredField validators automatically on the UI).

Thanks for your help.


Solution

  • I got it, the 2nd time that I'm answering to my own post :) Next time I'll do a deeper research before.

    For those of you that may encounter the same problem. You have to specify the ValidationProperty attribute on your server control's class. For instance if your server control exposes a property "Text" which is displayed to the user and which should also be validated, you add the following:

    [ValidationProperty("Text")]
    

    Then it should work.