Wednesday, March 14, 2007

ASP.NET Label Control Aligning Text

A fellow developer at work today was struggling to find away to perform what should be a fairly simple task. He was attempting to align some text in an asp:label control to the right. As their is no extremely obvious align like property some confusion resulted. After a quick bit of research and trial and error we worked it out.

1) Make sure the asp:label has a fixed width. This way aligning to the right is actually meaningful.

2) In your page_load or other relevant code section insert a piece of code similar to the following:
Label1.Style.Add("text-align", "right");

note that this can also be accessed like so:
Label1.Attributes.CssStyle.Add("text-align", "right");

The attributes element is also worth exploring for your own research, it can provide an enumerator for all keys associated with the current control.

No comments: