When to use runat=“server” on normal HTML

后端 未结 5 682
北荒
北荒 2020-12-28 16:09

Is it ever appropriate to use runat=\"server\" on a standard HTML element instead of a true ASP.NET control? I have full control over setting the html/text of the normal el

5条回答
  •  独厮守ぢ
    2020-12-28 16:20

    Both are ASP.NET server controls. The ones corresponding to HTML elements are in the System.Web.UI.HtmlControls namespace, and the web controls are in the System.Web.UI.WebControls namespace.

    The HTML controls are more light-weight and correspond exactly to an HTML element, while the web controls have more features and can be rendered as different HTML elements depending on the browser capabilities and the settings of the control.

    A HTML control renders as a single HTML element, while a web control is rendered as zero or more HTML elements. The Literal control for example isn't rendered as an element, it only outputs its text. There are other controls that doesn't render any elements by themselves, like the Repeater and PlaceHolder controls. On the other hand, the CheckBoxList control for example is rendered as several HTML element, a table as container, and input elements for each checkbox inside it.

    An example of a control that is rendered using different elements is the TextBox control, which will be rendered either as an input or a textarea element depending on its TextMode property.

    The web controls have more features, but also uses more resources. They have more properties and support things like themes and data binding. Many of the web controls put data in the ViewState, which is sent as part of the page. If you are not careful, the ViewState can get quite large, and affect the loading time of the page.

提交回复
热议问题