I've been using ASP.NET for a while using Visual Studio 2003, but I still have a huge lingering blind spot.
My application requires that the text in a label be constructed at runtime and be centered or right justified. If I use a web controls label, apparently I can set the text at runtime but I can't figure out how to right align it. If I use a HTML label,
apparently I can align it but I can't set the text at runtime.
Now if I was developing a classic desktop VB.NET app, then a label could have the TextAlign property set to MiddleRight as well has set the text at runtime.
I also just recently upgraded my ASP.NET app to use Visual Studio 2005, but this issue appears to be the same.
Certainly you can justify a runtime modifiable text label in ASP.NET so what am I missing?
davidgreenbe
Member
53 Points
40 Posts
How can I Right justify a web controls label in ASP.NET?
Nov 29, 2006 12:43 PM|LINK
I've been using ASP.NET for a while using Visual Studio 2003, but I still have a huge lingering blind spot.
My application requires that the text in a label be constructed at runtime and be centered or right justified. If I use a web controls label, apparently I can set the text at runtime but I can't figure out how to right align it. If I use a HTML label, apparently I can align it but I can't set the text at runtime.
Now if I was developing a classic desktop VB.NET app, then a label could have the TextAlign property set to MiddleRight as well has set the text at runtime.
I also just recently upgraded my ASP.NET app to use Visual Studio 2005, but this issue appears to be the same.
Certainly you can justify a runtime modifiable text label in ASP.NET so what am I missing?
Thanks,
David
Asp.Net 2.0 ASP.NET
serbach
Member
296 Points
97 Posts
Re: How can I Right justify a web controls label in ASP.NET?
Nov 29, 2006 02:06 PM|LINK
David,
I'm working on a web form now that has a lot of <asp:label> controls. I created a style sheet with a class for my labels:
.InfoLabel {
height: 18px;
width: 40px;
font-family: Arial, Helvetica, Sans-Serif;
font-size: 11px;
background-color: #e0d8e8;
border-style: none;
text-align: right;
}
Then I added the following in the <asp:label> attributes:
CssClass="InfoLabel"
Seems to do the trick.
Steve Erbach
Neenah, WI
http://www.TheTownCrank.com
ecbruck
All-Star
98783 Points
9691 Posts
Moderator
Re: How can I Right justify a web controls label in ASP.NET?
Nov 29, 2006 02:08 PM|LINK
Sure you can, simply use a stylesheet with two different classes for both left and right alignment like so:
.RightAligned { text-align: right; } .LeftAligned { text-align: left; }Then simply change the CssClass property of the Label to the appropiate class. Here's a little test if you want to use it:
ASPX
CODE_BEHIND
Microsoft MVP - ASP.NET
davidgreenbe
Member
53 Points
40 Posts
Re: How can I Right justify a web controls label in ASP.NET?
Nov 29, 2006 03:09 PM|LINK
Thanks serbach and ecbruck,
It seems odd and a bit immature that I can't just set a property for the alignment - but oh well I will use the stylesheet
David
alcsharp
Member
496 Points
106 Posts
Re: How can I Right justify a web controls label in ASP.NET?
Nov 29, 2006 06:03 PM|LINK
I you don't want to use the style-sheet then you can also set the style property of the label this way:
Label1.Style[
"text-align"] = "right";