1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Text;
5 using System.Web;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8
9 namespace Annsa.Controls
10 {
11 [DefaultProperty("Text")]
12 [ToolboxData("<{0}:AnnsaTextBox runat=server></{0}:AnnsaTextBox>")]
13 public class AnnsaTextBox : System.Web.UI.WebControls.TextBox
14 {
15 private const string _maxLengthAttributeName = "exMaxLen";
16
17 public AnnsaTextBox() : base()
18 {
19 }
20
21 override protected void OnPreRender(EventArgs e)
22 {
23 // Ensure default work takes place
24 base.OnPreRender(e);
25
26 // Configure TextArea support
27 // The System.Web.UI.WebControls.TextBox does not
28 // honour the "MaxLength" attribute when the
29 // "Multiline" textmode is selected.
30 if ( ( this.TextMode == TextBoxMode.MultiLine ) && ( this.MaxLength > 0 ) )
31 {
32 // If we haven't already, include the supporting
33 // script that limits the content of textareas.
34 this.Page.ClientScript.RegisterClientScriptResource(typeof(AnnsaTextBox),
35 "Annsa.Controls.TextBoxSupport.js");
36
37 // Add an expando attribute to the rendered control which sets
38 // its maximum length (using the MaxLength Attribute)
39 // Using RegisterExpandoAttribute maintains XHTML compliance on
40 // the rendered page.
41 //
42 // Where there is a ScriptManager on the parent page, use it
43 // to register the attribute - to ensure the control
44 // works in partial updates (like an AJAX UpdatePanel)
45 if ( ScriptManager.GetCurrent(this.Page) != null)
46 {
47 ScriptManager.RegisterExpandoAttribute( this,
48 this.ClientID,
49 _maxLengthAttributeName,
50 this.MaxLength.ToString(),
51 true);
52 }
53 else
54 {
55 this.Page.ClientScript.RegisterExpandoAttribute(this.ClientID,
56 _maxLengthAttributeName,
57 this.MaxLength.ToString());
58 }
59
60 // Now bind the onkeydown, oninput and onpaste events to
61 // script that we'll inject in the parent page.
62 this.Attributes.Add("onkeydown","javascript:return LimitInput(this, event);" );
63 this.Attributes.Add("oninput", "javascript:return LimitInput(this, event);" );
64 this.Attributes.Add("onpaste", "javascript:return LimitPaste(this, event);" );
65 }
66 }
67 }
68 }
69