fitting label dimensions to string length

M

Mike P

I have a method that is returning a string which can be of any length.
Once this string is returned I want to display it on screen in a label
control. How can I make the label control's height and width be
flexible so that it always fits the length of the string it contains?

<asp:Label ID="lblSQL" runat="server" Height="600" Width="600"
BackColor="blue" ForeColor="white" Font-Names="Verdana"
Font-Size="Small"></asp:Label>

protected void Page_Load(object sender, EventArgs e)
{
ViewState["CountGUID"] = Request.QueryString["GUID"];

string strCountSQL =
GetCountSql(ViewState["CountGUID"].ToString());

lblSQL.Text = strCountSQL;
}
 
A

Alberto Poblacion

Mike P said:
I have a method that is returning a string which can be of any length.
Once this string is returned I want to display it on screen in a label
control. How can I make the label control's height and width be
flexible so that it always fits the length of the string it contains?

<asp:Label ID="lblSQL" runat="server" Height="600" Width="600"
BackColor="blue" ForeColor="white" Font-Names="Verdana"
Font-Size="Small"></asp:Label>

Don't specify any Height or Width. Just assign the Text of the label. It
will merely render it as HTML into the containing page. It is up to the
browser to wrap it around as needed in a flexible way so that the whole text
will be accomodated on the label. This will happen without your needing to
do anything special, unless your HTML markup has placed the label inside an
inflexible container, in which case you will have to modify that design.
This is all done on the HTML design; there is nothing to do on the C# side,
which is what this forum is about.
 
I

Ignacio Machin ( .NET/ C# MVP )

I have a method that is returning a string which can be of any length.
Once this string is returned I want to display it on screen in a label
control.  How can I make the label control's height and width be
flexible so that it always fits the length of the string it contains?

<asp:Label ID="lblSQL" runat="server" Height="600" Width="600"
BackColor="blue" ForeColor="white" Font-Names="Verdana"
Font-Size="Small"></asp:Label>

protected void Page_Load(object sender, EventArgs e)
        {
            ViewState["CountGUID"] = Request.QueryString["GUID"];

            string strCountSQL =
GetCountSql(ViewState["CountGUID"].ToString());

            lblSQL.Text = strCountSQL;
        }

*** Sent via Developersdexhttp://www.developersdex.com***

If you do not specify the width it will ocupy as much as it needs or
can, If the label is inside a control (like a TD or a DIV) with an
especific width it will occupy as most as the size of the containing
tag.
 

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

Top