views:

30

answers:

1

I am creating a custom control which has some properties on it. The problem is that the control isn't valid without those properties being set and there aren't suitable default values for them.

How can I make sure that they are being set in the ASP.NET markup when being included on the page? Is there some kind of validation event that can be hooked into?

For example, the following control:

  public class TestControl: Control
  {
    public string Source { get; set; }
  }

At design/compile time there should be an error if the control is used without setting the Source property:

<Prototype:TestControl runat="server"></Prototype:JavaScriptInclude>

I know I could check this at runtime, but it would be nice to have some early checking going on as it could be overlooked if the validation is deferred until runtime.

+1  A: 

Short answer: you cannot.

Controls should have default behavior if properties aren't set. Only way to facilitate this is by bypassing the adding of the control in the aspx page, and do all this in your codebehind where you'll have the default language constructs like constructors. But I assume you know that path :-)

Jan Jongboom
Thanks, I am just going to override OnInit and do the validation there. Not perfect but acceptable.
Paul