Rovan

      一个犁牛半块田,收也凭天,荒也凭天, 清茶淡饭饱三餐,早也香甜,晚也香甜, 布衣得暖胜丝绵,长也可穿,短也可穿, 草舍茅屋有几间,行也安然,待也安然, 雨过天青驾小船,鱼在一边,酒在一边, 夜归儿女话灯前,今也有言,古也有言, 日上三竿我独眠,请是神仙,我是神仙.

首页 新随笔 联系 订阅 管理

 我们很多时候需要在一行上显示一段说明文字,而由于Web页面宽度的不确定性,我们任意调节其宽度后,常常搞得文字撑出页面或者折成好多行。通过使用CSS,我们可以限制为一行的宽度,并使多余的字符隐藏。为了方便,做成一个小Web控件来使用。

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace cnblogs.birdshome.WebControls
{
    /**//**//**//// <summary>
    /// Summary description for AutoLabel.
    /// </summary>
    [DefaultProperty("Text"),
        ToolboxData("<{0}:AutoLabel runat=server></{0}:AutoLabel>")]
    public class AutoLabel : System.Web.UI.WebControls.Label
    {
        protected override void CreateChildControls()
        {
            base.CreateChildControls ();
            this.Width = Unit.Percentage(100);
            this.Attributes["onmouseover"] =
                 "if ( this.clientWidth < this.scrollWidth ) this.title = this.innerText; else this.title = '';";
            this.Attributes.CssStyle["white-space"] = "nowrap";
            this.Attributes.CssStyle["overflow"] = "hidden";
            this.Attributes.CssStyle["text-overflow"] = "ellipsis";
        }
    }
}
AutoLabel继承至Label控件,默认宽度为"100%",当把AutoLabel放入容器类元素中后,其内容的宽度受容器大小自动调整。并且当AutoLabel出现"..."号后,鼠标放在上面,其ToolTip会自动显示器完整内容。

posted on 2007-08-09 11:44  Ruxuan  阅读(138)  评论(0)    收藏  举报