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

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

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

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

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

ContractedBlock.gif ExpandedBlockStart.gif 附 AutoTextBox 控件源码 #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.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.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.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        [DefaultValue(
200)]
InBlock.gif        
public int MaxHeight
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
object obj = ViewState["MaxHeight"];
InBlock.gif                
return obj == null ? 200 : (int)obj;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ViewState[
"MaxHeight"= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [DefaultValue(
60)]
InBlock.gif        
public int MinHeight
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
object obj = ViewState["MinHeight"];
InBlock.gif                
return obj == null ? 60 : (int)obj;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ViewState[
"MinHeight"= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void OnPreRender(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.Attributes["minHeight"= this.MinHeight.ToString();
InBlock.gif            
if ( this.Height == Unit.Empty )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.Height = this.MinHeight;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.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.gifContractedSubBlock.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.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string strCode;
InBlock.gif            
if ( this.MaxHeight <= 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strCode 
= "this.style.height=Math.max(this.minHeight,this.scrollHeight)+(this.offsetHeight-this.clientHeight)";
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.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.gifContractedSubBlock.gif            
dot.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/archive/2004/12/29/83338.html

转载于:https://www.cnblogs.com/dagon007/archive/2005/03/17/120123.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值