YouTip LogoYouTip

Prop Webcontrol Literal Text

## ASP.NET Literal Text Property The `Text` property is a core property of the ASP.NET `Literal` web control. It is used to get or set the text content displayed by the control. Unlike the `Label` control, the `Literal` control does not wrap its content in any surrounding HTML tags (such as `` or `
`). It renders the plain text or HTML markup directly to the page, making the `Text` property highly efficient for injecting raw content or dynamic HTML. --- ## Definition and Usage The `Text` property specifies the text or HTML markup to be rendered on the page. ### HTML Encoding Behavior How the `Text` property handles HTML characters depends on how the property is set: * **Declarative Attribute:** If you set the `Text` attribute directly within the ASP.NET markup (e.g., ``), the text is **HTML-decoded** before it is displayed. * **Programmatic or Inner Text:** If you set the property programmatically in the code-behind (e.g., `Literal1.Text = "..."`) or place the text directly between the opening and closing tags of the control, the text is **not** HTML-decoded. It is rendered exactly as provided. --- ## Syntax ### Declarative Syntax ```xml ``` ### Property Values | Value | Description | | :--- | :--- | | `string` | A string value representing the text or HTML markup to be displayed in the `Literal` control. | --- ## Code Examples ### Example 1: Basic Usage (Declarative) The following example demonstrates how to set the `Text` property directly in the ASP.NET markup. ```xml
``` ### Example 2: Setting the Text Programmatically (Code-Behind) You can dynamically update the `Text` property using C# or VB.NET in your code-behind file. **ASPX Markup:** ```xml
``` **C# Code-Behind (Default.aspx.cs):** ```csharp protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // Set the Text property programmatically LiteralMessage.Text = "Database connection successful!"; } } ``` --- ## Key Considerations ### 1. Literal vs. Label * Use **`Literal`** when you want to output raw text or HTML directly to the client browser without any extra styling or wrapper elements. * Use **`Label`** when you need to apply CSS styles, as it renders as an HTML `` element which supports the `CssClass` and `Style` properties. ### 2. Security Warning (XSS) Because the `Literal` control renders its `Text` property directly to the page without automatic HTML encoding (when set programmatically), it is vulnerable to **Cross-Site Scripting (XSS)** attacks if you display untrusted user input. If you are displaying user-submitted data, ensure you encode it first: ```csharp // Safe rendering of user input LiteralMessage.Text = Server.HtmlEncode(txtUserInput.Text); ```
← Prop Webcontrol Panel BackimagProp Webcontrol Listitem Enabl β†’