Autosize of label

J

James Wong

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
 
S

Saurabh Nandu

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

James Wong

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 said:
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 said:
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
 
S

Steven Cheng[MSFT]

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
 
J

James Wong

Hi Steven,

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

Regards,
James Wong
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top