我們很多時(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 VEVb.birdshome.WebControls
{
/**//// <summary>
/// Summary description for AutoLabel.
/// </summary>
[Default 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)把AutoLabel放入容器類元素中后,其內(nèi)容的寬度受容器大小自動(dòng)調(diào)整。并且當(dāng)AutoLabel出現(xiàn)"..."號(hào)后,鼠標(biāo)放在上面,其ToolTip會(huì)自動(dòng)顯示器完整內(nèi)容。 如下圖:

新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注