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

介绍了一个自定义Web控件AutoLabel,它继承自Label控件并利用CSS实现文本的自动换行及省略号显示效果,同时提供工具提示展示全部文本。
我们很多时候需要在一行上显示一段说明文字,而由于Web页面宽度的不确定性,我们任意调节其宽度后,常常搞得文字撑出页面或者折成好多行emsad.gif。通过使用CSS,我们可以限制为一行的宽度,并使多余的字符隐藏。为了方便,做成一个小Web控件来使用。
   
None.gifusing System;
None.gifusing System.Web.UI;
None.gifusing System.Web.UI.WebControls;
None.gifusing System.ComponentModel;
None.gif
None.gifnamespace cnblogs.birdshome.WebControls
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
InBlock.gif    
/// Summary description for AutoLabel.
ExpandedSubBlockEnd.gif    
/// </summary>
InBlock.gif    [DefaultProperty("Text"), 
InBlock.gif        ToolboxData("<{0}:AutoLabel runat=server></{0}:AutoLabel>")]
InBlock.gif    public class AutoLabel : System.Web.UI.WebControls.Label
ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
InBlock.gif        protected override void CreateChildControls()
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            base.CreateChildControls ();
InBlock.gif            this.Width = Unit.Percentage(100);
InBlock.gif            this.Attributes["onmouseover"] = 
InBlock.gif                 "if ( this.clientWidth < this.scrollWidth ) this.title = this.innerText; else this.title = '';";
InBlock.gif            this.Attributes.CssStyle["white-space"] = "nowrap";
InBlock.gif            this.Attributes.CssStyle["overflow"] = "hidden";
InBlock.gif            this.Attributes.CssStyle["text-overflow"] = "ellipsis";
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif    }
ExpandedBlockEnd.gif}

    AutoLabel继承至Label控件,默认宽度为"100%",当把AutoLabel放入容器类元素中后,其内容的宽度受容器大小自动调整。并且当AutoLabel出现"..."号后,鼠标放在上面,其ToolTip会自动显示器完整内容。 如下图:

    AutoLabel.gif

    enjoy it!

本文转自博客园鸟食轩的博客,原文链接:http://www.cnblogs.com/birdshome/,如需转载请自行联系原博主。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值