e-MEE
e-MEE

Reputation: 508

ASP.NET Label text align

How can I align every row to the right? This doesn't work:

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

Upvotes: 3

Views: 65029

Answers (6)

Dennis Xavier
Dennis Xavier

Reputation: 109

To align a label/textbox from code behind, you can use like this:

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

Upvotes: 1

Christine
Christine

Reputation: 9

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>

Upvotes: 0

Tym Pollack
Tym Pollack

Reputation: 125

When you add your label in the .aspx page, declare it with 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 during run-time, your best bet is to change the CssClass property of the Label.

Label1.CssClass = "right_align";

In your CSS:

.right_align { text-align: right; }

Upvotes: 9

YoryeNathan
YoryeNathan

Reputation: 14542

In C#, as pseudo code 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 string padding:

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.."

Upvotes: 1

SmartestVEGA
SmartestVEGA

Reputation: 8909

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

Upvotes: 0

Zachary Knight
Zachary Knight

Reputation: 86

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

Upvotes: 3

Related Questions