• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
西门吹雪
非我伤春春伤我,风一阵,雨一阵,好景不在哀残痕。 非我厌世世厌我,闲言来,碎语去,红颜憔悴心头冷。
博客园    首页    新随笔    联系   管理    订阅  订阅

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

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

    AutoLabel.gif
    enjoy it!

 

posted @ 2007-07-20 16:09  西门吹雪  阅读(140)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3