Darthg8r
Darthg8r

Reputation: 12685

ServerControl versus UserControl

I'd like to hear some reasons for using a ServerControl opposed to a UserControl. I've found that I probably overuse UserControls.

My list looks something like this:

Pro UserControl

  1. Easily modified. Need to add a class attribute to an element, hack it out in html.
  2. Quick and easy to create initial view. Everyone can write simple html right?

Pro ServerControl

  1. Performance. No html parsing.
  2. Flexibility. Control rendering down to a gnat's behind.
  3. Reusability. Compile it and stick it in the GAC for later use. Or, sell it.

Anything that I'm missing?

Upvotes: 3

Views: 1332

Answers (3)

Ruben
Ruben

Reputation: 15555

The following two advantages of server controls come to mind:

  • support for inheritance (because the actual user control at runtime inherits from your code behind, you can't inherit from it anymore)
  • detailed control over the tag's parsing (should nested tags be treated as child controls or properties, etc.)
  • support for control adapters (declarative, e.g. in a .browsers file)
  • support for tag mapping (if you're making your own controls that's often very useful though)

However, user controls do have an additional edge because they're templated:

  • support for databinding expressions

Upvotes: 5

Joel Coehoorn
Joel Coehoorn

Reputation: 416149

One other consideration is whether you want visual studio designer support at the time you're building the control or at the time you're using the control on the page. It really irks me that you only get one or the other, but not both.

Upvotes: 2

Mark Brackett
Mark Brackett

Reputation: 85685

Performance. No html parsing

I wouldn't expect that to be much of a runtime difference - the .ascx should get compiled on first use as well. Perhaps there's some slight overhead saved by not checking the file for modifications - but I can't think of much else.

Upvotes: 1

Related Questions