自动处理过长字符串显示的Web控件

王朝other·作者佚名  2006-11-24
窄屏简体版  字體: |||超大  

我们很多时候需要在一行上显示一段说明文字,而由于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会自动显示器完整内容。 如下图:

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航