Last post Nov 29, 2006 06:03 PM by alcsharp
Nov 29, 2006 12:43 PM|davidgreenbe|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?
Nov 29, 2006 02:06 PM|serbach|LINK
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:
font-family: Arial, Helvetica, Sans-Serif;
Then I added the following in the <asp:label> attributes:
Seems to do the trick.
Nov 29, 2006 02:08 PM|ecbruck|LINK
Sure you can, simply use a stylesheet with two different classes for both left and right alignment like so:
Then simply change the CssClass property of the Label to the appropiate class. Here's a little test if you want to use it:
<asp:label id="lbl" runat="server" text="This is a test!" width="500" /><br />
<asp:linkbutton id="btnAlignLeft" runat="server" commandargument="Left" onclick="AlignLabel"
text="align left" />
<asp:linkbutton id="btnAlignRight" runat="server" commandargument="Right" onclick="AlignLabel"
text="align right" />
protected void AlignLabel(object sender, EventArgs e)
LinkButton btn = sender as LinkButton;
lbl.CssClass = (btn.CommandArgument == "Left") ? "LeftAligned" : "RightAligned";
Nov 29, 2006 03:09 PM|davidgreenbe|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
Nov 29, 2006 06:03 PM|alcsharp|LINK
I you don't want to use the style-sheet then you can also set the style property of the label this way: