views:

68

answers:

2

I have read through the information in this question: http://stackoverflow.com/questions/22084/asp-net-aspxxx-controls-versus-standard-html but am still rather confused.

The situation was I was asked to do a web project where I made a wizard. When I was done with the project everyone asked why I had used an <asp:Wizard...>. I thought this was what was being asked for, but apparently not, so after this I was led to believe that server controls were just prototyping tools.

However, the next project I did my DB queries through C# code-behind and loaded the results via html. I was then asked why I had not used a gridview and a dataset.

Does anyone have a list of pros and cons why they would choose to use specific html controls over specific server controls and why? I guess I'm looking for a list... what server controls are okay to use and why?

EDIT: I guess this question is open ended, so I'll clarify a few more specific questions...

  1. Is it okay to use very simple controls such as asp:Label or do these just end up wasting space? It seems like it would be difficult to access html in the code behind otherwise.

  2. Are there a few controls that should just never be used?

  3. Does anyone have a good resource that will show me pros and cons of each control?

+2  A: 

Server Controls Rely on ViewState, Restrict Flexibility

Server controls were powerful tools to introduce WinForms developers to web development. The main benefit that Server Controls provide is a sense of statefullness and an event-driven development model. However, the state of web development has been maturing since the introduction of WebForms and server controls, which as a whole are being challenged with an increasingly critical view.

The main issue with Server Controls is that they try to abstract the behavior of the web too much, and in doing so, rely on ViewState data and server resources to perform their magic. ViewState data, when used excessively, can severely bloat the size of your page, resulting in performance problems.

Now it is possible to opt out of ViewState, but by then it probably is better to simply resort to regular HTML controls.

With HTML controls, you have precise control over the content of the web page and you have greater flexibility in what behaviors you want on your web page. Server controls offer limited client-side flexibility and force a lot of work to be performed on the server that can easily be performed in the browser with a good JavaScript framework.

Technobabble
Very helpful - thanks for all the resources.
Brandi
Thanks for the vote. There is still plenty of debate for both sides of the argument. On one hand, server controls are easier to set up, but on the other hand, HTML controls provide more fine-grained control and are likely to perform better. It really depends on your situation. As a rule of thumb: if you are just going to be displaying flat, read-only content, go with HTML controls or scaled-back server controls such as the Literal or Repeater instead of the Label and GridView. Also, try to disable ViewState by default and only opt-in where it provides a clear advantage.
Technobabble
+1  A: 
  1. Is it okay to use very simple controls such as asp:Label or do these just end up wasting space? It seems like it would be difficult to access html in the code behind otherwise.

If you need to access the control in server side code, use a server control. If not, don't.

  1. Are there a few controls that should just never be used?

Not in my (mid level) experience. The Wizard control, once you know how to use it, works as advertised (imagine coding all the features yourself, then do the math). I've used it and it resulted in a smooth and functional multi-page sign up form with intermediate saving of data.

  1. Does anyone have a good resource that will show me pros and cons of each control?

I do not, but I'd recommend using the right control for the right situation. Study the differences between a Repeater and a GridView, for example, and use the best choice for your needs.

There of course are alternatives. For one, MVC is gaining momentum. Personally I have not yet committed to learning it. As far as interactive forms and AJAX goes, many .NET devs opt to use JQuery for any validation and any AJAX (UpdatePanels being easy to use and horribly inefficient), with JSON as the transport mechanism to the server side.

sweaver2112
Hmm... is it possible to access html controls from code behind if you absolutely needed to?
Brandi
Also, thanks for the opinion. I was wanting to know if anyone more experienced than I am with ASP .NET was a fan of using the controls.
Brandi
Brandi: yes. If you want a "regular" html control accessible from server side, you can do this: <div runat="server"></div>. (add the 'runat' attribute) Check here for a list:http://www.w3schools.com/aspnet/aspnet_refhtmlcontrols.asp You just won't get the event model you get with <asp:XXXX> controls.
sweaver2112