Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use HtmlControls vs WebControls

Tags:

asp.net

I like HtmlControls because there is no HTML magic going on... the asp source looks similar to what the client sees.

I can't argue with the utility of GridView, Repeater, CheckBoxLists, etc, so I use them when I need that functionality.

Also, it looks weird to have code that mixes and matches:

<asp:Button id='btnOK' runat='server' Text='OK' />
<input id='btnCancel' runat='server' type='button' value='Cancel' />

(The above case in the event you wanted to bind a server-side event listener to OK but Cancel just runs a javascript that hides the current div)

Is there some definitive style guide out there? Should HtmlControls just be avoided?

like image 938
Jimmy Avatar asked Sep 11 '08 23:09

Jimmy


People also ask

What is the difference between HTML control and web control?

Web Server Controls can detect the target browser's capabilities and render themselves accordingly. Server controls are easy to use and manage but HTML controls are not. Server control events are handled in the server side whereas HTML control events are handled in the client side browser only .

What is the difference between HTML controls and server controls in asp net?

Server controls can maintain data across requests using view state whereas HTML controls have no such mechanism to store data between requests. Server controls can detect browser automatically and adapt display of control accordingly whereas HTML controls can't detect browser automatically.

What is HTML control in asp net?

The HTML server controls are HTML elements that include a runat=server attribute. The HTML server controls have the same HTML output and the same properties as their corresponding HTML tags. In addition, HTML server controls provide automatic state management and server-side events.

What is a Web server control?

Web server controls include traditional form controls such as buttons and text boxes as well as complex controls such as tables. They also include controls that provide commonly used form functionality such as displaying data in a grid, choosing dates, displaying menus, and so on.


2 Answers

It might be useful to think of HTML controls as an option when you want more control over the mark up that ends up getting emitted by your page. More control in the sense that you want EVERY browser to see exactly the same markup.

If you create System.Web.UI.HtmlControls like:

<input id='btnCancel' runat='server' type='button' value='Cancel' />

Then you know what kind of code is going to be emitted. Even though most of the time:

<asp:Button id='btnCancel' runat='server' Text='Cancel' />

will end up being the same markup. The same markup is not always emitted for all WebControls. Many WebControls have built in adaptive rendering that will render different HTML based on the browser user agent. As an example a DataGrid will look quite different in a mobile browser than it will in a desktop browser.

Using WebControls as opposed to HtmlControls also lets you take advantage of ASP.NET v2.0 ControlAdapters which I believe only works with WebControls, this will allow you programatic config driven control over the markup that gets emitted.

This might seem more valuable when you consider that certain mobile browsers or WebTVs are going to want WML or completely different sets of markups.

like image 166
Tyler Avatar answered Sep 28 '22 10:09

Tyler


In my experience, there's very little difference. As Darren said, if you don't need server-side functionality, HTML controls are probably lower-impact.

And don't forget, you can bolt server-side functionality onto almost any HTML control just by adding a runat="server" directive and an ID to it.

like image 38
TheSmurf Avatar answered Sep 28 '22 10:09

TheSmurf