asp.net - How to change Label text programmatically

Change Label text programmatically
The Label class Represents a label control, which displays text on a Web page. The asp.net developers can use the Label control to display text in a set location on the web page. Unlike static text, asp.net c# developers can customize the displayed text through the Text property.

The following asp.net c# tutorial code demonstrates how we can programmatically change a Label control’s text. Here we used the Label web server control’s Text property to change its contained text programmatically.

The Label control Text property gets or sets the text content of the Label control. This property value is a String which is the text content of the control. The default value of this property is Empty.

The asp.net developers have to use the Text property to specify or determine the text content of the Label control. The Label control’s Text property is commonly used to programmatically customize the text that is displayed in the Label control.

The Text property can include HTML but the HTML will be passed unchanged to the browser. If the asp.net developers want the browser to display HTML markup as plain text, they can use the HtmlEncode method.
LabelText.aspx

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        Label1.Text = "You clicked the Button";
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to set, change Label text programmatically</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Navy">Label Example: Text</h2>
        <asp:Label 
             ID="Label1" 
             runat="server"
             Text="Click button for change This text."
             ForeColor="Crimson"
             BackColor="LightPink"
             Font-Size="X-Large"
             >
        </asp:Label>
        <br /><br />
        <asp:Button 
             ID="Button1" 
             runat="server" 
             ForeColor="IndianRed"
             Text="Change Label Text"
             OnClick="Button1_Click"
             Font-Bold="true"
             />
    </div>
    </form>
</body>
</html>
More asp.net examples