views:

146

answers:

3

In web forms application, for server code, when use WebControls and when use HtmlControls? For example if I want write some text inside of span tag, should I use:

<span id="someid" runat="server"></span>

or

<asp:Label id="someid" runat="server"></asp:Label>
A: 

I would use the span approach. Whatever server control you use it will finally render as an html control. If your functionality can be done using an html control better use that.. For a server control like data grid you might have to code more to achieve those functionality by using an html table. In that case you can use a server control.

rahul
+2  A: 

The main difference is that HtmlControls only provide a way of addressing a part of the page during the page cycle, whereas WebControls are stateful.

In your example, if you assign some value to the Label text, it will keep it across PostBacks.

In my experience is far better to use HtmlControls if you can, they are much more lightweight and they don't fill up your ViewState. Do use WebControls when you need them to be stateful.

For example, you might want to use a Label for a page title, because you can only assign the value once (typically in Page_OnLoad inside a if (!IsPostBack) block). You might want to use an HTML span to provide some status feedback (where the status is updated at each postback, for example).

Sklivvz
I'm not too sure that HTMLControls are not "stateful". If you populate the InnerText of a SPAN once during runtime, it will be persisted across subsequent postbacks.
o.k.w
HtmlControls are not "stateful", meaning, only in context that its values are not sent back and forth using page viewstate. But, obviously you can get the value of a SPAN since the DOM is available in the server side.
Vadi
I don't think that the DOM of the previous rendering of a page is available to the next page cycle. You would get the original DOM, possibly modified where you use WebControls. On the other hand the contents of a SPAN are not posted back during postback.
Sklivvz
A: 

one behaviour of asp:button is it always render as input type=submit and asp:imagebutton always render as input type=image

mangokun