Paul
Paul

Reputation: 3163

Design time validation of custom ASP.NET server controls

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.

Upvotes: 0

Views: 287

Answers (1)

Jan Jongboom
Jan Jongboom

Reputation: 27322

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 :-)

Upvotes: 1

Related Questions