Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebControl vs HtmlControl. Cos and pros using them in web forms application

Tags:

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>
like image 642
Kamarey Avatar asked Aug 23 '09 13:08

Kamarey


1 Answers

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).

like image 125
Sklivvz Avatar answered Oct 13 '22 08:10

Sklivvz