[RESOLVED] Align label text in WebForms
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: [RESOLVED] Align label text in WebForms

  1. #1
    Join Date
    Jan 2010
    Posts
    130

    [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);
    }

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    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 );

  3. #3
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    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'.

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Align label text in WebForms

    Ahhh, I missed the "Webforms" part of the title .

  5. #5
    Join Date
    Jan 2010
    Posts
    130

    Question 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?

  6. #6
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    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 05:37 AM. Reason: Spelling mistake...

  7. #7
    Join Date
    Jan 2010
    Posts
    130

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center