What's the Literal control used for and what's the difference to the Label Control in asp.net?

后端 未结 7 1929
耶瑟儿~
耶瑟儿~ 2021-01-04 02:06

What Literal control is used for in asp.net? and What is the difference between them and Label control?

7条回答
  •  花落未央
    2021-01-04 02:58

    The major difference is that the Label Control adds the span tag to the text (property) you set, allowing to apply a style to it:

    My Label text
    

    The Literal Control allows you to render any kind of content. You can use it to render scripts, hmtl and any other type of document content. It doesn't change the string you provide in the Text property.

    Note: the Label control allows you to render straight HTML too, but it puts all your text in span tags as mentioned. So, for rendering large HTML portions a Literal control is the way to go.

    P.S.: In HTML there is a tag. If you use the AssociatedControlId property of the Label control, it will render as HTML (thanks to Ray for pointing that out.)

    For example:

    
    Input First Name:
    
    
    

    Will render as:

    
    
    

    See also here on W3 Schools.

提交回复
热议问题