C# ASP.NET 标签文本对齐
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9806280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET Label text align
提问by e-MEE
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(" ", " ");
采纳答案by Tym Pollack
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; }
回答by Zachary Knight
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.
我不明白你为什么要从背后的代码中进行对齐。将标签放在页面上具有特定对齐设置的控件中。如果您在后面的代码中创建标签,则创建一个具有特定对齐方式的控件,可以以编程方式将标签插入其中。
回答by SmartestVEGA
Click on the label, go to properties, see there in an align attribute, set to Right1
单击标签,转到属性,在对齐属性中查看,设置为 Right1
回答by SimpleVar
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.."
回答by Christine
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>
回答by Dennis
To align a label/textbox from code behind, you can use like this:
要从后面的代码对齐标签/文本框,您可以像这样使用:
Label1.Text = "<center>Your Text to print here..</center>";

