When to use HtmlControls vs WebControls

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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.

Method 3

well… i wouldn’t use an html control if you don’t need to do anything on it on the server. i would do

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

fin.

Method 4

By adding runat=”server” you can get access to any HTML controls in server side..
and I believe HTML controls are less weight compared to ASP.NET server controls..


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x