高度自适应textBox控件
cs:
1
附 AutoTextBox 控件源码#region 附 AutoTextBox 控件源码
2
using System;
3
using System.Web.UI;
4
using System.Web.UI.WebControls;
5
using System.ComponentModel;
6
7
namespace WebExcel.UI.WebControls
8

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

附 AutoTextBox 控件源码#region 附 AutoTextBox 控件源码 2
using System; 3
using System.Web.UI; 4
using System.Web.UI.WebControls; 5
using System.ComponentModel; 6
7
namespace WebExcel.UI.WebControls 8


{ 9

/**//**//**//// <summary> 10
/// Summary description for AutoLengthTextBox. 11
/// </summary> 12
[DefaultProperty("Text"), 13
ToolboxData("<{0}:AutoTextArea runat=server></{0}:AutoTextArea>")] 14
public class AutoTextArea : System.Web.UI.WebControls.TextBox 15

{ 16
[DefaultValue(200)] 17
public int MaxHeight 18

{ 19
get 20

{ 21
object obj = ViewState["MaxHeight"]; 22
return obj == null ? 200 : (int)obj; 23
} 24
set 25

{ 26
ViewState["MaxHeight"] = value; 27
} 28
} 29
30
[DefaultValue(60)] 31
public int MinHeight 32

{ 33
get 34

{ 35
object obj = ViewState["MinHeight"]; 36
return obj == null ? 60 : (int)obj; 37
} 38
set 39

{ 40
ViewState["MinHeight"] = value; 41
} 42
} 43
44
protected override void OnPreRender(EventArgs e) 45

{ 46
this.Attributes["minHeight"] = this.MinHeight.ToString(); 47
if ( this.Height == Unit.Empty ) 48

{ 49
this.Height = this.MinHeight; 50
} 51
else 52

{ 53
this.Height = (int)Math.Max(this.MinHeight, this.Height.Value); 54
} 55
base.OnPreRender (e); 56
} 57
58

/**//**//**//// <summary> 59
/// Render this control to the output parameter specified. 60
/// </summary> 61
/// <param name="output"> The HTML writer to write out to </param> 62
protected override void Render(HtmlTextWriter output) 63

{ 64
string strCode; 65
if ( this.MaxHeight <= 0 ) 66

{ 67
strCode = "this.style.height=Math.max(this.minHeight,this.scrollHeight)+(this.offsetHeight-this.clientHeight)"; 68
} 69
else 70

{ 71
strCode = "this.style.height=(this.scrollHeight>200)?200:Math.max(this.minHeight,this.scrollHeight)+(this.offsetHeight-this.clientHeight)"; 72
} 73
base.Attributes["onpropertychange"] = strCode; 74
// base.Attributes["onfocus"] = "this.height=this.height"; 75
if ( base.Rows == 0 ) 76

{ 77
base.Rows = 1; 78
} 79
base.TextMode = TextBoxMode.MultiLine; 80
base.Render(output); 81
} 82
} 83
} 84
#endregion
浙公网安备 33010602011771号