-
January 18th, 2010, 10:03 AM
#1
[RESOLVED] Align label text in WebForms
I have programmatically created a label and would now like to align the text to the center. I am using C# with ASP.NET so cannot use the usual Windows.Form methods.
foreach (string grade in eNumGradesList)
{
lblENumGrade = new Label();
cellENumGrade = new TableCell();
// Position cells and specify details
lblENumGrade.Text = grade;
this.lblENumGrade.Text = this.lblENumGrade.Text.PadLeft(this.lblENumGrade.Text.Length + 25); // NOT WORKING
//lblENumGrade.Text.PadLeft(20); //NOT WORKING EITHER
cellENumGrade.Controls.Add(lblENumGrade);
cellENumGrade.BorderColor = System.Drawing.Color.Blue;
rowHeader.Controls.Add(cellENumGrade);
}
-
January 18th, 2010, 01:12 PM
#2
Re: Align label text in WebForms
Ok, so to center a label in a rectangle you just need to divide...
Code:
lblTest = new Label( );
// ... set width, height, other properties
Point location = new Point(
( this.width / 2 ) - ( lblTest / 2 ),
( this.height / 2 ) - ( lblTest.Height / 2 )
);
lblTest.Location = location;
this.Controls.Add( lblTest );
-
January 18th, 2010, 07:35 PM
#3
Re: Align label text in WebForms
Another option would be to use CSS. You can specify the CssClass property of the label to class you define in the stylesheet. In the definition of the style you can specify the 'text-align' property and set it to 'center'.
-
January 18th, 2010, 08:36 PM
#4
Re: Align label text in WebForms
Ahhh, I missed the "Webforms" part of the title .
-
January 19th, 2010, 05:11 AM
#5
Re: Align label text in WebForms
Yes I was thinking of specifying it in the stylesheet but I do not know how to specify the CssClass programmatically. I thought this could only be done in ASP.NET?
-
January 19th, 2010, 06:36 AM
#6
Re: Align label text in WebForms
You will still be using ASP.NET to specify the stylesheet programmatically. ASP.NET should be used in your case to render the page i.e. produce the markup elements with their attributes. Anything related to positioning should ideally be done through CSS. If you have an ASP.NET control you should be able to specify the CssClass. It is just another property like 'Text', etc. If that doesn't work you should be able to specify the 'class' through the attributes collection.
Code:
// Something like this...
lblTest.Attributes.Add("class", "centerAligned");
// Or this...
lblTest.CssClass = "centerAligned";
Last edited by nelo; January 19th, 2010 at 06:37 AM.
Reason: Spelling mistake...
-
January 19th, 2010, 06:51 AM
#7
Re: Align label text in WebForms
Thank you, I will try that.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|