How to encode html in a Label in example - label encode html
Label is an web server control. mainly label control is used to display formatted text in web page. label Text property allow us to specify text to display in web page. label server control can render some html code when displaying text in web page such as bold, italic, anchor etc.

in this example we displayed html tag in a label control as text. to do this we take help from server.HtmlEncode() method. HtmlEncode method applies html encoding to a specified string. so HtmlEncode() method allow us to encode some special characters to their HTML-encoded equivalent before render the label text in web browser. finally label control display html tags in web page.

the following c# example code demonstrate us how can we display html inside a label control in an application.

<%@ Page Language="C#" AutoEventWireup="true"%>  
<!DOCTYPE html>
<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
        Label1.Text = Server.HtmlEncode("<h3>This is a heading</h3>");
        Label1.Text += "<br />";
        Label1.Text += Server.HtmlEncode("<p>This is a paragraph.</p>");
        Label1.Text += "<br />";
        Label1.Text += Server.HtmlEncode("<button type='button'>Click Me!</button>");
        Label1.Text += "<br />";
        Label1.Text += Server.HtmlEncode("<meta name='author' content='innee'>");
<html xmlns="">      
<head id="Head1" runat="server">      
    <title> example - label encode html</title>
    <form id="form1" runat="server">      
        <h2 style="color:MidnightBlue; font-style:italic;">      
   example - label encode html
        <hr width="550" align="left" color="Gainsboro" />      
            Text="sample label to display html code."
        <br /><br /><br />
            Text="label encode html"  
More examples