简体   繁体   English

ASP.NET标签文本对齐

[英]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;");

When you add your label in the .aspx page, declare it with a CSS class or with style="text-align: right;". 在.aspx页面中添加标签时,请使用CSS类或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. 如果要在运行时更改对齐,最好的办法是更改Label的CssClass属性。

Label1.CssClass = "right_align";

In your CSS: 在你的CSS中:

.right_align { text-align: right; }

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. 如果您在后面的代码中创建标签,则创建一个具有特定对齐的控件,该控件可以通过编程方式将标签插入其中。

单击标签,转到属性,在align属性中查看,设置为Right1

In C#, as pseudo code for asp.Net: 在C#中,作为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.."

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>

要从后面的代码中对齐标签/文本框,您可以像这样使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM