Autosize of label

Discussion in 'ASP .Net Web Controls' started by James Wong, Jul 12, 2004.

  1. James Wong

    James Wong Guest

    Dear all,

    I would like to know how I can set the width of label according to the
    length of content. Since the label content is dynamic, length of label
    should change from time to time to avoid word-warp. I don't want to pre-set
    a "very long" width at design time because there is background color for the
    label and I think it's not good-looking enough to have a long color tail
    after the real content.

    If there is no built-in function to do that, I want to know how I can make
    the calculation by myself according to the font name, type, size and label
    content.

    Thanks for your attention and kindly help!

    Regards,
    James Wong
     
    James Wong, Jul 12, 2004
    #1
    1. Advertising

  2. Hi James,

    Are you using GridLayout in VS.NET? When GridLayout is used to design the pages it fixes the length of the controls, to emulate like a Windows Form.


    There are two things you can do. On the page level change the GridLayout to FlowLayout from Properties Manager for the page. This will make all the controls on the page flow. (It does not remove the poistions on older controls, so they will still appear fixed sized).

    Second option is to change to HTML mode and then locate the label control. Once you have located the right label control, go ahead and remove the *style* attribute. The style attibute has settings to define the label to displayed in constrained space.

    --
    Regards,
    Saurabh Nandu
    Master C#, the easy way...
    [ www.MasterCSharp.com ]



    "James Wong" wrote:

    > Dear all,
    >
    > I would like to know how I can set the width of label according to the
    > length of content. Since the label content is dynamic, length of label
    > should change from time to time to avoid word-warp. I don't want to pre-set
    > a "very long" width at design time because there is background color for the
    > label and I think it's not good-looking enough to have a long color tail
    > after the real content.
    >
    > If there is no built-in function to do that, I want to know how I can make
    > the calculation by myself according to the font name, type, size and label
    > content.
    >
    > Thanks for your attention and kindly help!
    >
    > Regards,
    > James Wong
    >
    >
    >
     
    Saurabh Nandu, Jul 12, 2004
    #2
    1. Advertising

  3. James Wong

    James Wong Guest

    Hi Saurabh,

    Thanks for your reply! But I think I make you misunderstand my question.
    What I want to know is label control on web form. On my web form, there're
    some label controls to show tiny information which is dynamic in run-time.
    On Windows Form, there's AutoSize property on label control but not on web
    form. So I want to emulate by myself.

    Regards,
    James Wong

    "Saurabh Nandu" <> ¦b¶l¥ó
    news: ¤¤¼¶¼g...
    > Hi James,
    >
    > Are you using GridLayout in VS.NET? When GridLayout is used to design the

    pages it fixes the length of the controls, to emulate like a Windows Form.
    >
    >
    > There are two things you can do. On the page level change the GridLayout

    to FlowLayout from Properties Manager for the page. This will make all the
    controls on the page flow. (It does not remove the poistions on older
    controls, so they will still appear fixed sized).
    >
    > Second option is to change to HTML mode and then locate the label control.

    Once you have located the right label control, go ahead and remove the
    *style* attribute. The style attibute has settings to define the label to
    displayed in constrained space.
    >
    > --
    > Regards,
    > Saurabh Nandu
    > Master C#, the easy way...
    > [ www.MasterCSharp.com ]
    >
    >
    >
    > "James Wong" wrote:
    >
    > > Dear all,
    > >
    > > I would like to know how I can set the width of label according to the
    > > length of content. Since the label content is dynamic, length of label
    > > should change from time to time to avoid word-warp. I don't want to

    pre-set
    > > a "very long" width at design time because there is background color for

    the
    > > label and I think it's not good-looking enough to have a long color tail
    > > after the real content.
    > >
    > > If there is no built-in function to do that, I want to know how I can

    make
    > > the calculation by myself according to the font name, type, size and

    label
    > > content.
    > >
    > > Thanks for your attention and kindly help!
    > >
    > > Regards,
    > > James Wong
    > >
    > >
    > >
     
    James Wong, Jul 12, 2004
    #3
  4. Hi James,

    As for the Webform Label control, its abit different from the Winform since
    the webform control is acutally rendered as Html element whose position and
    size will denpend on the page's layout setting (flowlayout or gridlayout).
    The ASP.NET Label control, is actually rendered out as an <span>...</span>
    html element. And if we don't set the Label's width it 'll adjust its width
    automatically according the Text(it contains)'s length. For example:
    <asp:Label id="lblOne" >dfdsfsdfdsfdsf</asp:Label>

    So just don't set the "Width" attribute for the TextBox and let it auto
    adjust itself.

    In addition, there're also some one else who use serverside apis to measure
    the width of the Text in the TextBox and dynamically change its width. Here
    is a former thread discussing on this:

    http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&threadm=b75a01c1a032$1a
    b89d40%24b1e62ecf%40tkmsftngxa04&rnum=1&prev=/groups%3Fhl%3Den%26lr%3D%26ie%
    3DUTF-8%26q%3Dasp.net%2B%2BLabel%2Bautosize


    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    Get Preview at ASP.NET whidbey
    http://msdn.microsoft.com/asp.net/whidbey/default.aspx
     
    Steven Cheng[MSFT], Jul 13, 2004
    #4
  5. James Wong

    James Wong Guest

    Hi Steven,

    Thanks for your reply and it works in so simple way!

    Regards,
    James Wong

    "Steven Cheng[MSFT]" <> ¦b¶l¥ó
    news:gm% ¤¤¼¶¼g...
    > Hi James,
    >
    > As for the Webform Label control, its abit different from the Winform

    since
    > the webform control is acutally rendered as Html element whose position

    and
    > size will denpend on the page's layout setting (flowlayout or gridlayout).
    > The ASP.NET Label control, is actually rendered out as an <span>...</span>
    > html element. And if we don't set the Label's width it 'll adjust its

    width
    > automatically according the Text(it contains)'s length. For example:
    > <asp:Label id="lblOne" >dfdsfsdfdsfdsf</asp:Label>
    >
    > So just don't set the "Width" attribute for the TextBox and let it auto
    > adjust itself.
    >
    > In addition, there're also some one else who use serverside apis to

    measure
    > the width of the Text in the TextBox and dynamically change its width.

    Here
    > is a former thread discussing on this:
    >
    >

    http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&threadm=b75a01c1a032$1a
    >

    b89d40%24b1e62ecf%40tkmsftngxa04&rnum=1&prev=/groups%3Fhl%3Den%26lr%3D%26ie%
    > 3DUTF-8%26q%3Dasp.net%2B%2BLabel%2Bautosize
    >
    >
    > Regards,
    >
    > Steven Cheng
    > Microsoft Online Support
    >
    > Get Secure! www.microsoft.com/security
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
    >
    > Get Preview at ASP.NET whidbey
    > http://msdn.microsoft.com/asp.net/whidbey/default.aspx
    >
     
    James Wong, Jul 13, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?ZGFycmVu?=

    autosize a label

    =?Utf-8?B?ZGFycmVu?=, Apr 19, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    617
    =?Utf-8?B?ZGFycmVu?=
    Apr 19, 2007
  2. Ryou kaihou
    Replies:
    1
    Views:
    423
    Eric Moreau
    Jun 14, 2007
  3. Ryou kaihou
    Replies:
    1
    Views:
    194
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
    Jun 14, 2007
  4. Ryou kaihou
    Replies:
    0
    Views:
    149
    Ryou kaihou
    Jun 14, 2007
  5. Knut Krueger
    Replies:
    8
    Views:
    341
    Knut Krueger
    Aug 20, 2013
Loading...

Share This Page