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

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

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

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

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

ExpandedBlockStart.gif #region 附 AutoTextBox 控件源码
InBlock.gif using System;
InBlock.gif using System.Web.UI;
InBlock.gif using System.Web.UI.WebControls;
InBlock.gif using System.ComponentModel;
InBlock.gif
InBlock.gif namespace WebExcel.UI.WebControls
ExpandedSubBlockStart.gif{
ExpandedSubBlockStart.gif     ///   <summary>
InBlock.gif    
///  Summary description for AutoLengthTextBox.
ExpandedSubBlockEnd.gif    
///   </summary>
InBlock.gif    [DefaultProperty("Text"), 
InBlock.gif        ToolboxData("<{0}:AutoTextArea runat=server></{0}:AutoTextArea>")]
InBlock.gif     public  class AutoTextArea : System.Web.UI.WebControls.TextBox
ExpandedSubBlockStart.gif    {
InBlock.gif        [DefaultValue(200)]
InBlock.gif         public  int MaxHeight
ExpandedSubBlockStart.gif        {
InBlock.gif             get
ExpandedSubBlockStart.gif            {
InBlock.gif                 object obj = ViewState["MaxHeight"];
InBlock.gif                 return obj ==  null ? 200 : ( int)obj;
ExpandedSubBlockEnd.gif            }
InBlock.gif             set
ExpandedSubBlockStart.gif            {
InBlock.gif                ViewState["MaxHeight"] = value;
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        [DefaultValue(60)]
InBlock.gif         public  int MinHeight
ExpandedSubBlockStart.gif        {
InBlock.gif             get
ExpandedSubBlockStart.gif            {
InBlock.gif                 object obj = ViewState["MinHeight"];
InBlock.gif                 return obj ==  null ? 60 : ( int)obj;
ExpandedSubBlockEnd.gif            }
InBlock.gif             set
ExpandedSubBlockStart.gif            {
InBlock.gif                ViewState["MinHeight"] = value;
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif         protected  override  void OnPreRender(EventArgs e)
ExpandedSubBlockStart.gif        {
InBlock.gif             this.Attributes["minHeight"] =  this.MinHeight.ToString();
InBlock.gif             if (  this.Height == Unit.Empty )
ExpandedSubBlockStart.gif            {
InBlock.gif                 this.Height =  this.MinHeight;
ExpandedSubBlockEnd.gif            }
InBlock.gif             else
ExpandedSubBlockStart.gif            {
InBlock.gif                 this.Height = ( int)Math.Max( this.MinHeight,  this.Height.Value);
ExpandedSubBlockEnd.gif            }
InBlock.gif             base.OnPreRender (e);
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockStart.gif         ///   <summary>  
InBlock.gif        
///  Render this control to the output parameter specified.
InBlock.gif        
///   </summary>
ExpandedSubBlockEnd.gif        
///   <param name="output">  The HTML writer to write out to  </param>
InBlock.gif         protected  override  void Render(HtmlTextWriter output)
ExpandedSubBlockStart.gif        {
InBlock.gif             string strCode;
InBlock.gif             if (  this.MaxHeight <= 0 )
ExpandedSubBlockStart.gif            {
InBlock.gif                strCode = "this.style.height=Math.max(this.minHeight,this.scrollHeight)+(this.offsetHeight-this.clientHeight)";
ExpandedSubBlockEnd.gif            }
InBlock.gif             else
ExpandedSubBlockStart.gif            {
InBlock.gif                strCode = "this.style.height=(this.scrollHeight>200)?200:Math.max(this.minHeight,this.scrollHeight)+(this.offsetHeight-this.clientHeight)";
ExpandedSubBlockEnd.gif            }
InBlock.gif             base.Attributes["onpropertychange"] = strCode;
InBlock.gif             //  base.Attributes["onfocus"] = "this.height=this.height";
InBlock.gif
             if (  base.Rows == 0 )
ExpandedSubBlockStart.gif            {
InBlock.gif                 base.Rows = 1;
ExpandedSubBlockEnd.gif            }
InBlock.gif             base.TextMode = TextBoxMode.MultiLine;
InBlock.gif             base.Render(output);
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif    }
ExpandedSubBlockEnd.gif}
ExpandedBlockEnd.gif #endregion


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

目录
相关文章
Winform中Textbox、NumericUpDown等修改高度,禁止输入数字或内容的实现
Winform中的Textbox、NumericUpDown控件通常在单行的情况下,无法直接通过`Height`属性修改高度,但很多时候我们需要调整其高度,使其显示的更加合理,主要介绍三种方法...
1703 0
|
17天前
textarea文本框根据输入内容自动适应高度
textarea文本框根据输入内容自动适应高度
12 0
|
4月前
|
C++
[Qt5&控件] Label控件显示文本内容(字符串和整数)
[Qt5&控件] Label控件显示文本内容(字符串和整数)
67 0
[Qt5&控件] Label控件显示文本内容(字符串和整数)
|
前端开发 测试技术
Easyui datagrid 设置内容超过单元格宽度时自动换行显示
Easyui datagrid 设置内容超过单元格宽度时自动换行显示
275 0
|
数据安全/隐私保护 C++
2.8 输入控件(一)
2.8 输入控件(一)
2.8 输入控件(一)
|
存储 前端开发 C++
2.8 输入控件(三)
2.8 输入控件(三)
2.8 输入控件(三)
|
C#
WPF TextBox/TextBlock 文本超出显示时,文本靠右显示
原文:WPF TextBox/TextBlock 文本超出显示时,文本靠右显示 文本框显示 文本框正常显示: 文本框超出区域显示:    实现方案 判断文本框是否超出区域 请见《TextBlock IsTextTrimmed 判断文本是否超出》 设置文本布局显示 1.
1564 0
|
C#
【WPF】设置TextBox内容为空时的提示文字
原文:【WPF】设置TextBox内容为空时的提示文字 ...
4520 0