我們很多時(shí)候需要在一行上顯示一段說(shuō)明文字,而由于web頁(yè)面寬度的不確定性,我們?nèi)我庹{(diào)節(jié)其寬度后,常常搞得文字撐出頁(yè)面或者折成好多行。通過(guò)使用css,我們可以限制為一行的寬度,并使多余的字符隱藏。為了方便,做成一個(gè)小web控件來(lái)使用。
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控件,默認(rèn)寬度為"100%",當(dāng)把a(bǔ)utolabel放入容器類元素中后,其內(nèi)容的寬度受容器大小自動(dòng)調(diào)整。并且當(dāng)autolabel出現(xiàn)"..."號(hào)后,鼠標(biāo)放在上面,其tooltip會(huì)自動(dòng)顯示器完整內(nèi)容。 如下圖:
   
新聞熱點(diǎn)
疑難解答
圖片精選