How to align Label text to right in asp.net

asp.net example - label text align right
Label is an asp.net web server control. label control has no built in property to set or change its text align. so we need to take advantage from css style to apply text align in label control. label has a property named Style. using this Style property's Add() method we can add many css feature to label control.

in this example we added css text-align property and its value 'right' to label control by using its Style property. we added this style to label displayed text by a button click event at run time and programmatically.

the following asp.net c# example code demonstrate us how can we apply label text align to right in an asp.net application.
label-text-align-right.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>  
    
<!DOCTYPE html>
      
<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        Label1.Style.Add("text-align", "right");
    }  
</script>      
      
<html xmlns="http://www.w3.org/1999/xhtml">      
<head id="Head1" runat="server">      
    <title>asp.net example - label text align right</title>      
</head>      
<body>      
    <form id="form1" runat="server">      
    <div>      
        <h2 style="color:MidnightBlue; font-style:italic;">      
            asp.net example - label text align right
        </h2>      
        <hr width="550" align="left" color="Gainsboro" />      
        <asp:Label       
            ID="Label1"       
            runat="server"      
            Font-Size="X-Large"
            Text="sample label to test text align."
            BorderColor="Green"
            BorderWidth="1"
            Width="450"    
            >      
        </asp:Label>      
        <br /><br /><br />
        <asp:Button   
            ID="Button1"   
            runat="server"   
            Text="label text align right"  
            OnClick="Button1_Click"
            Height="40"  
            Font-Bold="true"  
            />  
    </div>      
    </form>      
</body>      
</html>
More asp.net examples