ASP.Net: Literal vs Label

后端 未结 4 866
无人及你
无人及你 2020-12-07 10:12

I just wanted to hear some authorities on when and where you should use a LITERAL control over a LABEL.

As I understand it, the difference

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-07 10:57

    Difference b/w Label and Literal Control in asp.net

    In almost all ways a Literal control is the same as a Label control. Both of these controls are used to display Text on a webform. (The Text property can be set in the HTML or in the code-behind.)

    The biggest difference is that the Label control wraps the text in a span when rendered. Any style that is applied to the Label control, will be rendered using the style property of the span.

    For example, the following HTML

    
    

    Will be rendered as

    Label Text

    A Literal control doesn't output any surrounding tags, so the Text is displayed as is:

    For example, the following HTML

    
    

    will be rendered as

    Literal Control Text

    So if you want to apply any styles to a than use Label control otherwise use the Literal control. Because of this, the Literal control is a light weight control, when compared with the Label control.

    FYI: The inheritance hierarchy for Literal control class is (Object => Control => Literal), where as for the Label control, the hierarchy is (Object => Control => WebControl=> Label)

提交回复
热议问题