自动适应输入内容高度的TextBox控件 (转)

来源:互联网 发布:小米云服务擦除数据 编辑:程序博客网 时间:2024/05/20 04:30

前些日子有人问怎么样动态的改变TextBox的高度,在网上找了下,有个现成的,现转载下:

关于Web开发上面UI布局的问题,我上次介绍了一个可以自动适应输入内容宽度的TextBox控件,它可以解决在布局时预留控件大小和用户数入内容多少上的矛盾。但是由于那个控件被限制了只能做为单行输入使用:(,在输入大块文本时就力不从心了,那么就再做一个可自动适应高度的TextBox。

    原理和那个适应宽度的TextBox查不多,只是这个反而更加简单,因为在高度方向上增长不会破坏页面的整体布局效果(宽度上的如果在页内会挤走别的元素的),所以就不需要使用Agent TextBox来作为实际录入的容器了,直接把<TextArea>增高就行了。

    响应onpropertychange事件,同步内容和<TextArea>的高度。当然如果完全根据内容增高可能也会因为内容太多而变得难看,就设置了一个最大高度限制属性。控件效果如下:

   
最大高度为200px的AutoTextBox Demo:
最大高度为200px但初始高度为3rows的AutoTextBox Demo:
高度增长无限制的AutoTextBox Demo:
    如果控件的MaxHeight属性小于或等于0,那么增长高度无限制。

  1. #region 附 AutoTextBox 控件源码 
  2. using System; 
  3. using System.Web.UI; 
  4. using System.Web.UI.WebControls; 
  5. using System.ComponentModel; 
  6. namespace WebExcel.UI.WebControls 
  7.     /**//// <summary> 
  8.     /// Summary description for AutoLengthTextBox. 
  9.     /// </summary> 
  10.     [DefaultProperty("Text"),  
  11.         ToolboxData("<{0}:AutoTextArea runat=server></{0}:AutoTextArea>")] 
  12.     public class AutoTextArea : System.Web.UI.WebControls.TextBox 
  13.     { 
  14.         [DefaultValue(200)] 
  15.         public int MaxHeight 
  16.         { 
  17.             get 
  18.             { 
  19.                 object obj = ViewState["MaxHeight"]; 
  20.                 return obj == null ? 200 : (int)obj; 
  21.             } 
  22.             set 
  23.             { 
  24.                 ViewState["MaxHeight"] = value; 
  25.             } 
  26.         } 
  27.         [DefaultValue(60)] 
  28.         public int MinHeight 
  29.         { 
  30.             get 
  31.             { 
  32.                 object obj = ViewState["MinHeight"]; 
  33.                 return obj == null ? 60 : (int)obj; 
  34.             } 
  35.             set 
  36.             { 
  37.                 ViewState["MinHeight"] = value; 
  38.             } 
  39.         } 
  40.         protected override void OnPreRender(EventArgs e) 
  41.         { 
  42.             this.Attributes["minHeight"] = this.MinHeight.ToString(); 
  43.             if ( this.Height == Unit.Empty ) 
  44.             { 
  45.                 this.Height = this.MinHeight; 
  46.             } 
  47.             else 
  48.             { 
  49.                 this.Height = (int)Math.Max(this.MinHeight, this.Height.Value); 
  50.             } 
  51.             base.OnPreRender (e); 
  52.         } 
  53.         /**//// <summary>  
  54.         /// Render this control to the output parameter specified. 
  55.         /// </summary> 
  56.         /// <param name="output"> The HTML writer to write out to </param> 
  57.         protected override void Render(HtmlTextWriter output) 
  58.         { 
  59.             string strCode; 
  60.             if ( this.MaxHeight <= 0 ) 
  61.             { 
  62.                 strCode = "this.style.height=Math.max(this.minHeight,this.scrollHeight)+(this.offsetHeight-this.clientHeight)"
  63.             } 
  64.             else 
  65.             { 
  66.                 strCode = "this.style.height=(this.scrollHeight>200)?200:Math.max(this.minHeight,this.scrollHeight)+(this.offsetHeight-this.clientHeight)"
  67.             } 
  68.             base.Attributes["onpropertychange"] = strCode; 
  69.             // base.Attributes["onfocus"] = "this.height=this.height"; 
  70.             if ( base.Rows == 0 ) 
  71.             { 
  72.                 base.Rows = 1; 
  73.             } 
  74.             base.TextMode = TextBoxMode.MultiLine; 
  75.             base.Render(output); 
  76.         } 
  77.     } 
  78. #endregion 
原创粉丝点击