Question

I'm creating my first .net website using c#.

I was wondering what the best way is to set web controls dynamically. I've seen examples where controls are set in the aspx page and others where it is set in code.

Looking for advice as to why one is preferred over the other, or is it just a matter of choice?

Was it helpful?

Solution

This is a very broad question and the answer is mostly preference based.

For me, if I'm writing something without a lot of HTML, but and mostly functional back-end processing (or even heavily javascript based), I'll write a server control and create everything in code. If my output is heavily decorated HTML, then I will most certainly use a UserControl. There are several caveats that limit your ability to use the more beginner-friendly UserControl, including inclusion in a toolbox, export to a library, etc.

OTHER TIPS

I think that generally it's a matter of personal preference.

Personally I prefer to declare controls in the .aspx page - I just think it's tidier. However, if the properties of a control are dynamic and depend on the results of code executed in the .aspx.cs, then I will set up the properties of the control there instead.

You'll develop your own opinion of the by the time you finish your first site, so no need to think about this too much now.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top