4

I have a custom ASP.NET server control CustomControl with a property attribute Path.

If the Path is not explicitly specified, then I want an exception to be thrown.

For example,

<myControls:CustomControl Path="somedirectory/someotherdirectory/somefile.ext" runat="server" />

should compile, and

<myControls:CustomControl runat="server" /> should throw an exception.

I realize I can do this in the getter of the Path property, but is there some attribute that necessitates this?

Update

Is there any mechanism for validating the values of the property attributes other than using in the getter methods of the code-behind properties?

1

4 Answers 4

7

You can check this in init event of the custom control, and throw an exception.

Update

There is; Check it in init event. As init of control's called after host's (page or user control) init event. So you can throw an exception if its null or empty

Sign up to request clarification or add additional context in comments.

Comments

1

I don't think this should be a compile time check.

After all, the property value might be supplied in the code behind during page_load or a similiar event.

1 Comment

for my use case this is not a concern.
0

There is not an attribute that necessitates this. The best place to do this is in the getter of the Path property.

2 Comments

You could always write your own attribute though
Writing attributes doesn't do much good if nothing processes them.
0

No, there's no way to force the page framework to do this. You need to do it in your code at runtime and raise an exception if the value wasn't provided. Otherwise we'd never run into the "crap, I forgot the runat=server part" error :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.