using System; using System.Web; using System.Web.UI; using System.Text; using System.Web.UI.WebControls; using System.ComponentModel; namespace Expoware { [DefaultEvent("EnterPressed")] public class PostTextBox : TextBox, IPostBackEventHandler { // ********************************************************************* // Ctor public PostTextBox() : base() { _fBrowserCapsTested = false; CausesValidation = true; } // ********************************************************************* // ********************************************************************* // Private members private bool _fBrowserCapsTested; private bool _fCanAddScript; // ********************************************************************* // ********************************************************************* // PROPERTY CanPostBack: // Generates the client script code to post back when Enter is pressed public bool CanPostBack { get {return Convert.ToBoolean(ViewState["CanPostBack"]);} set {ViewState["CanPostBack"] = value;} } // ********************************************************************* // ********************************************************************* // PROPERTY CausesValidation: // Indicates whether validation is performed when Enter key is pressed public bool CausesValidation { get {return Convert.ToBoolean(ViewState["CausesValidation"]);} set {ViewState["CausesValidation"] = value;} } // ********************************************************************* // ********************************************************************* // EVENT EnterPressed: // Occurs when the Enter key is pressed and the page posts back public event EventHandler EnterPressed; // ********************************************************************* // ********************************************************************* // OVERRIDE Load: // Ensures that attributes and client script code are in place protected override void OnLoad(EventArgs e) { base.OnLoad (e); // Is PostBack needed? if (!CanPostBack) { Attributes.Remove("onkeypress"); return; } // Verify it is at least IE4 if (!_fBrowserCapsTested) _fCanAddScript = TestBrowser(); // If needed, generate the script and add the onkeypress handler if (_fCanAddScript) { RegisterCaptureEnterScript(); string js = "CaptureEnter(this)"; // Modify the code to support client-side validation if (CausesValidation && Page.Validators.Count > 0) { js = "javascript:" + GetValidatedPostbackCode(); } Attributes["onkeypress"] = js; } } // ********************************************************************* // ********************************************************************* // PRIVATE TestBrowser: // Checks the browser's capabilities (i.e., at least IE4) private bool TestBrowser() { HttpBrowserCapabilities caps = Page.Request.Browser; _fBrowserCapsTested = true; if (caps.Browser.ToUpper().IndexOf("IE") > -1) { return caps.MajorVersion >3; } return false; } // ********************************************************************* // ********************************************************************* // PRIVATE GetValidatedPostbackCode: // Return a Javascript code that enables postback for validated input private string GetValidatedPostbackCode() { StringBuilder sb = new StringBuilder(""); sb.Append("{if (typeof(Page_ClientValidate) != 'function' "); sb.Append("|| Page_ClientValidate()) "); sb.Append("CaptureEnter(this)}"); return sb.ToString(); } // ********************************************************************* // ********************************************************************* // PRIVATE RegisterCaptureEnterScript: // Generates and injects the script to capture the Enter key private void RegisterCaptureEnterScript() { // Build the Javascript code to capture the Enter key StringBuilder sb = new StringBuilder(""); sb.Append("