ASP.NET label text alignment

How can I align each line to the right? This does not work:

Label1.Text = String.Format("{0, 15}", "aaaaaaaa").Replace(" ", " ")
                 + "<br />"
                 + String.Format("{0, 15}", "bbb").Replace(" ", "&nbsp;");

      

+3


source to share


6 answers


When you add your label to the .aspx page, declare it using a CSS class or with style = "text-align: right;".

<asp:Label id="Label1" runat="server" width="100px" style="text-align: right;" />

      

If you want to change the alignment at runtime, your best bet is to change the CssClass property of the label.



Label1.CssClass = "right_align";

      

In your CSS:

.right_align { text-align: right; }

      

+9


source


I don't understand why you are trying to align behind the code. Place a label in a control on a page that has a specific alignment set. If you are creating a label in the code behind, create a control with a specific alignment, which can have a label programmatically inserted into it.



+3


source


Click on the label, go to properties, see there in the align attribute, set the value to Right1

+1


source


In C #, as pseudocode for asp.Net:

var label = new Label();
label.TextAlign = ContentAlignment.MiddleRight; // Aligns to right
label.RightToLeft = RightToLeft.Yes; // Changed direction to rtl (might reverse the meaning of TextAlignment

      

Or if you want to use adding lines:

string pad, aaaa = "aaaa";
pad = aaaa.PadLeft(6); // "  aaaa"
pad = aaaa.PadLeft(6, '-'); // "--aaaa"
pad = aaaa.PadRight(10); // "aaaa      "
pad = aaaa.PadLeft(6).PadRight(8); // "  aaaa  "
pad = aaaa.PadLeft(6).PadRight(8, '.'); // "  aaaa.."

      

+1


source


Or you could just do it this way.

<asp:TableCell HorizontalAlign="Right">
    <asp:Label ID="lblGrossPay" runat="server" Text="2, 375"></asp:Label>
</asp:TableCell>

      

+1


source


To align the label / textbox with the code behind you can use like this:

Label1.Text = "<center>Your Text to print here..</center>";

      

0


source







All Articles