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

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

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


 {
{ 9

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

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

 
         {
{ 19
 get
            get 20

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

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

 
         {
{ 33
 get
            get 34

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

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

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

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

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

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

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

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

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

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