自动处理过长字符串显示的Web控件
发布日期:2021-09-08 15:09:19 浏览次数:6 分类:技术文章

本文共 1080 字,大约阅读时间需要 3 分钟。

我们很多时候需要在一行上显示一段说明文字,而由于Web页面宽度的不确定性,我们任意调节其宽度后,常常搞得文字撑出页面或者折成好多行
emsad.gif。通过使用CSS,我们可以限制为一行的宽度,并使多余的字符隐藏。为了方便,做成一个小Web控件来使用。
   
None.gif
using System;
None.gif
using System.Web.UI;
None.gif
using System.Web.UI.WebControls;
None.gif
using System.ComponentModel;
None.gif
None.gif
namespace cnblogs.birdshome.WebControls
ExpandedBlockStart.gif
ContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gif
ContractedSubBlock.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.gif
ContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected 
override 
void CreateChildControls()
ExpandedSubBlockStart.gif
ContractedSubBlock.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/,如需转载请自行联系原博主。

转载地址:https://blog.csdn.net/weixin_34326429/article/details/90187976 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:跨域实现IFRAME自适应高度~续(终级)
下一篇:Android实例剖析笔记(四)

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年04月08日 02时04分52秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章