From: Argiris K. <be...@us...> - 2006-01-03 01:49:32
|
Update of /cvsroot/magicajax/magicajax/Core/UI/Controls/ClientEventControls In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9346/Core/UI/Controls/ClientEventControls Added Files: BaseClientEventWrapper.cs ClientEventControl.cs ClientEventTrigger.cs KeyClientEventWrapper.cs Log Message: -Client event handling controls -Various minor changes --- NEW FILE: ClientEventTrigger.cs --- using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.ComponentModel; namespace MagicAjax.UI.Controls { /// <summary> /// Captures a client event of a control. /// </summary> /// <remarks> /// The EventName property must be set to the client event (i.e. "focus","change", etc.) /// and the ControlID property must be set to the ID of the control whose event we want to /// capture. The AjaxCall that will be invoked is dependent at the ClientEventTrigger's /// placement, not the placement of ControlID's control. For example, if the ClientEventTrigger /// is inside an AjaxPanel and AjaxCall will be invoked even if the ControlID's control is /// not inside an AjaxPanel. /// /// The ClientEventTrigger must be inside the same NamingContainer (i.e. UserControl) /// as the ControlID's control. /// </remarks> [Designer("MagicAjax.UI.Design.ClientEventTriggerDesigner, MagicAjax"), ParseChildrenAttribute(true), PersistChildren(false), ToolboxData("<{0}:ClientEventTrigger runat=server></{0}:ClientEventTrigger>")] public class ClientEventTrigger : ClientEventControl, INonHtmlHolder { private string _controlID = String.Empty; private string _eventName = String.Empty; public event EventHandler Invoke; [Bindable(false), Category("Behaviour"), DefaultValue("")] public string ControlID { get { return _controlID; } set { _controlID = value; } } [Bindable(false), Category("Behaviour"), DefaultValue("")] public string EventName { get { return _eventName; } set { _eventName = value; } } protected override void HandleEvent(string eventName, string argument) { OnInvoke (EventArgs.Empty); } protected override void Render(HtmlTextWriter writer) { if ( Page != null ) { string attachScriptFormat = @"<script type='text/javascript'> <!-- elem=document.getElementById('{0}'); fn=function(e) {{__doPostBack('{1}',e.type+';');}}; if (elem.addEventListener) elem.addEventListener('{2}',fn,false); else elem.attachEvent('on{2}',fn); // --> </script>"; if (ControlID == null || ControlID == String.Empty) throw new MagicAjaxException(String.Format("ControlID property for trigger {0} is not set.", ClientID)); if (EventName == null || EventName == String.Empty) throw new MagicAjaxException(String.Format("EventName property for trigger {0} is not set.", ClientID)); Control con = this.NamingContainer.FindControl(ControlID); if ( con == null ) throw new MagicAjaxException(String.Format("The ControlID '{0}' does not exist or is not in the same NamingContainer as trigger {1}", ControlID, ClientID)); if ( ! Page.IsStartupScriptRegistered("__TRIGGER" + this.ClientID) ) Page.RegisterStartupScript ("__TRIGGER" + this.ClientID, String.Format(attachScriptFormat, con.ClientID, this.UniqueID, EventName)); writer.Write("<span id=\"{0}\"></span>", ClientID); } } protected virtual void OnInvoke (EventArgs e) { if (Invoke != null) Invoke (this, e); } } } --- NEW FILE: ClientEventControl.cs --- using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace MagicAjax.UI.Controls { /// <summary> /// Base class for controls that expose and handle client events. /// </summary> public abstract class ClientEventControl : WebControl, IPostBackEventHandler { protected abstract void HandleEvent (string eventName, string argument); #region IPostBackEventHandler Members public void RaisePostBackEvent(string eventArgument) { int index = eventArgument.IndexOf(';'); if ( index == -1 ) throw new MagicAjaxException(String.Format("Invalid eventArgument '{0}' for client event wrapper '{1}'.", eventArgument, ClientID)); HandleEvent (eventArgument.Substring(0, index), eventArgument.Substring(index + 1)); } #endregion public ClientEventControl() : base(HtmlTextWriterTag.Span) { } protected override void OnPreRender(EventArgs e) { base.OnPreRender (e); if (this.Page != null && Enabled) Util.CallPrivateMethod(this.Page, typeof(Page), "RegisterPostBackScript"); } } } --- NEW FILE: BaseClientEventWrapper.cs --- using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.ComponentModel; namespace MagicAjax.UI.Controls { /// <summary> /// Base class for controls that capture the client events of their inner controls. /// </summary> [Designer("MagicAjax.UI.Design.BaseClientEventWrapperDesigner, MagicAjax"), ParseChildrenAttribute(false), PersistChildren(true)] public abstract class BaseClientEventWrapper : ClientEventControl { protected abstract void AttachClientEvents (HtmlTextWriter writer); protected override void AddAttributesToRender(HtmlTextWriter writer) { base.AddAttributesToRender (writer); AttachClientEvents(writer); } protected virtual void AttachEvent (string eventName, string getArgumentScript, HtmlTextWriter writer) { if (getArgumentScript == null) writer.AddAttribute (eventName, String.Format("__doPostBack('{0}',event.type+';');", UniqueID)); else writer.AddAttribute (eventName, String.Format("arg={0};__doPostBack('{1}',event.type+';'+arg);", getArgumentScript, UniqueID)); } protected void AttachEvent (string eventName, HtmlTextWriter writer) { AttachEvent (eventName, null, writer); } } } --- NEW FILE: KeyClientEventWrapper.cs --- using System; using System.ComponentModel; using System.Web; using System.Web.UI; namespace MagicAjax.UI.Controls { #region Class KeyPressEventArgs public class KeyPressEventArgs : EventArgs { private char _keyChar; public char KeyChar { get { return _keyChar; } } public KeyPressEventArgs (char keyChar) { this._keyChar = keyChar; } } #endregion #region Class KeyEventArgs public class KeyEventArgs : EventArgs { private int _keyCode; private bool _alt, _control, _shift; public int KeyCode { get { return _keyCode; } } public bool Alt { get { return _alt; } } public bool Control { get { return _control; } } public bool Shift { get { return _shift; } } public KeyEventArgs (int keyCode, bool alt, bool control, bool shift) { this._keyCode = keyCode; this._alt = alt; this._control = control; this._shift = shift; } } #endregion public delegate void KeyPressEventHandler (object sender, KeyPressEventArgs e); public delegate void KeyEventHandler (object sender, KeyEventArgs e); /// <summary> /// Captures the KeyPress, KeyDown, and KeyUp client events of its inner controls. /// </summary> /// <remarks> /// The KeyPress event has the IE behaviour across all browsers (invoked when a character /// is typed or Enter is pressed). /// </remarks> [ToolboxData("<{0}:KeyClientEventWrapper runat=server>Insert controls to capture their OnKey events</{0}:KeyClientEventWrapper>")] public class KeyClientEventWrapper : BaseClientEventWrapper { public event KeyPressEventHandler KeyPress; public event KeyEventHandler KeyDown; public event KeyEventHandler KeyUp; protected override void HandleEvent(string eventName, string argument) { switch (eventName) { case "keypress" : OnKeyPress (new KeyPressEventArgs((char)Int32.Parse(argument))); break; case "keydown" : OnKeyDown (CreateKeyEventArgs(argument)); break; case "keyup" : OnKeyUp (CreateKeyEventArgs(argument)); break; default: throw new MagicAjaxException(String.Format("Invalid event name '{0}' captured by {1}.", eventName, ClientID)); } } protected override void AttachClientEvents(HtmlTextWriter writer) { if ( KeyPress != null ) writer.AddAttribute ("onkeypress", String.Format("__onKeyPress(event,'{0}');", UniqueID)); if ( KeyDown != null ) writer.AddAttribute ("onkeydown", String.Format("__onKeyUpDown(event,'keydown','{0}');", UniqueID)); if ( KeyUp != null ) writer.AddAttribute ("onkeyup", String.Format("__onKeyUpDown(event,'keyup','{0}');", UniqueID)); } protected override void OnPreRender(EventArgs e) { string onKeyScript = @"<script type='text/javascript'> <!-- function __onKeyPress(e,src) { if ('which' in e && e.keyCode!=13) { if (e.which>=32) __doPostBack(src,'keypress;'+e.which); } else { __doPostBack(src,'keypress;'+e.keyCode); } } function __onKeyUpDown(e,evtname,src) { __doPostBack(src,evtname+';'+e.keyCode+','+e.altKey+','+e.ctrlKey+','+e.shiftKey); } // --> </script>"; base.OnPreRender (e); if ( ! Page.IsClientScriptBlockRegistered("__ONKEYBLOCK") ) Page.RegisterClientScriptBlock("__ONKEYBLOCK", onKeyScript); } protected virtual KeyEventArgs CreateKeyEventArgs (string argument) { int keyCode; bool alt, control, shift; try { string[] args = argument.Split(','); keyCode = Int32.Parse(args[0]); alt = Boolean.Parse(args[1]); control = Boolean.Parse(args[2]); shift = Boolean.Parse(args[3]); return new KeyEventArgs(keyCode, alt, control, shift); } catch { throw new MagicAjaxException(String.Format("Invalide KeyEvent argument '{0}' for {1}.", argument, ClientID)); } } protected virtual void OnKeyPress (KeyPressEventArgs e) { if (KeyPress != null) KeyPress (this, e); } protected virtual void OnKeyDown (KeyEventArgs e) { if (KeyDown != null) KeyDown (this, e); } protected virtual void OnKeyUp (KeyEventArgs e) { if (KeyUp != null) KeyUp (this, e); } } } |