WebControl vs HtmlControl. Cos and pros use them in a web form application

In a web form application, for server code, when using WebControls and when using HtmlControls? For example, if I want to write text inside a span tag, I should use:

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

or

 <asp:Label id="someid" runat="server"></asp:Label> 
+5
source share
3 answers

The main difference is that HtmlControls provides only a way to address part of the page during the page loop, while WebControls have state.

In your example, if you assign a value to a Label tag, it will be stored in PostBacks.

In my experience it is much better to use HtmlControls, if possible, they are much lighter and they do not populate your ViewState. Use WebControls when you need them to be functional.

For example, you can use a shortcut for the page title because you can only assign a value once (usually in Page_OnLoad inside the if (!IsPostBack) ). You might want to use the HTML range to provide some feedback on the status (for example, when the status is updated with each postback).

+7
source

I would use the span approach. No matter which server control you are using, it will finally display as an html control. If your functionality can be accomplished using html control, better use this. For a server control, such as a data grid, you may need more code to achieve these functions using the html table. In this case, you can use the server control.

+1
source

one asp behavior: the button is always displayed as input type = submit and asp: imagebutton is always displayed as input type = image

-3
source

Source: https://habr.com/ru/post/899867/


All Articles