From: Dion O. <dol...@us...> - 2006-01-20 01:36:07
|
Update of /cvsroot/magicajax/magicajax/Core In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21237/magicajax/Core Modified Files: AjaxCallHelper.cs MagicAjax NET 2.0.csproj MagicAjax.csproj MagicAjaxModule.cs Util.cs Log Message: First support for scripts and hidden fields that were added during callback. Still beta! Index: AjaxCallHelper.cs =================================================================== RCS file: /cvsroot/magicajax/magicajax/Core/AjaxCallHelper.cs,v retrieving revision 1.19 retrieving revision 1.20 diff -C2 -d -r1.19 -r1.20 *** AjaxCallHelper.cs 11 Jan 2006 20:27:32 -0000 1.19 --- AjaxCallHelper.cs 20 Jan 2006 01:35:56 -0000 1.20 *************** *** 25,28 **** --- 25,29 ---- using System.Web.UI; using System.Collections; + using System.Collections.Specialized; using System.Text; *************** *** 307,310 **** --- 308,334 ---- /// <summary> + /// Produces the javascript that will add a script element to the page. + /// </summary> + /// <param name="scriptText">The script text</param> + /// <param name="scriptText">The script attributes</param> + public static void WriteAddScriptElementScript(string scriptText, NameValueCollection scriptAttributes) + { + StringBuilder sbuilder = new StringBuilder("new Array("); + for (int i = 0; i < scriptAttributes.Count; i++) + { + if (i > 0) + sbuilder.Append(","); + sbuilder.AppendFormat("\"{0}\",\"{1}\"", scriptAttributes.Keys[i], scriptAttributes[i]); + } + sbuilder.Append(")"); + Write(String.Format("AJAXCbo.AddScript({0},{1});\r\n", EncodeString(scriptText), sbuilder.ToString())); + } + + public static void WriteAddHiddenFieldScript(string fieldName, string fieldValue) + { + Write(String.Format("AJAXCbo.AddHiddenField(\"{0}\",{1});\r\n", fieldName, EncodeString(fieldValue))); + } + + /// <summary> /// Provides the javascript that will se the innerHTML of an element of the page. /// </summary> Index: Util.cs =================================================================== RCS file: /cvsroot/magicajax/magicajax/Core/Util.cs,v retrieving revision 1.23 retrieving revision 1.24 diff -C2 -d -r1.23 -r1.24 *** Util.cs 11 Jan 2006 20:27:32 -0000 1.23 --- Util.cs 20 Jan 2006 01:35:56 -0000 1.24 *************** *** 44,47 **** --- 44,48 ---- public static Regex FormElementOptionSelectedRegEx = new Regex(@"<option.*?(?<selected>selected=(""|'|)selected(""|'|)(\s+|(?=>))).*?>", _options); public static Regex ScriptPatternRegEx = new Regex(Util.ScriptPattern, RegexOptions.IgnoreCase); + public static Regex ScriptTagsRegEx = new Regex("<script\\s((?<attrname>[-\\w]+)=\"(?<attrvalue>.*?)\"\\s?)*\\s*>(?<script>.*?)</script>", _options); #endregion *************** *** 267,270 **** --- 268,298 ---- /// <summary> + /// Copies all fields (private and public) from the source object to + /// the destination object thereby performing shallow copy + /// Unlike shallow copy however, the objects do not need to be of the same type + /// and destination can be a decendant of source in the class heirarchy + /// </summary> + /// <param name="destination">object to copy fields on</param> + /// <param name="source">object to copy fields from</param> + public static void ForceShallowCopyOnObject(object destination, object source) + { + Type dstType = destination.GetType(); + Type srcType = source.GetType(); + // get all fields of source type + // to get all private fields loop over all base classes. + for (Type t = srcType; t != typeof(Object); t = t.BaseType) + { + System.Reflection.FieldInfo[] srcFields = t.GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public); + foreach (System.Reflection.FieldInfo fld in srcFields) + { + string fldName = fld.Name; + object fldValue = fld.GetValue(source); + System.Reflection.FieldInfo dstFld = t.GetField(fldName, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public); + dstFld.SetValue(destination, fldValue); + } + } + } + + /// <summary> /// Gets all child controls of a specific type. /// </summary> Index: MagicAjax.csproj =================================================================== RCS file: /cvsroot/magicajax/magicajax/Core/MagicAjax.csproj,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** MagicAjax.csproj 3 Jan 2006 01:49:09 -0000 1.10 --- MagicAjax.csproj 20 Jan 2006 01:35:56 -0000 1.11 *************** *** 203,206 **** --- 203,211 ---- /> <File + RelPath = "UI\Controls\AjaxHtmlForm.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File RelPath = "UI\Controls\AjaxHtmlImage.cs" SubType = "Code" Index: MagicAjax NET 2.0.csproj =================================================================== RCS file: /cvsroot/magicajax/magicajax/Core/MagicAjax NET 2.0.csproj,v retrieving revision 1.13 retrieving revision 1.14 diff -C2 -d -r1.13 -r1.14 *** MagicAjax NET 2.0.csproj 3 Jan 2006 01:49:09 -0000 1.13 --- MagicAjax NET 2.0.csproj 20 Jan 2006 01:35:56 -0000 1.14 *************** *** 124,127 **** --- 124,128 ---- <SubType>Code</SubType> </Compile> + <Compile Include="UI\Controls\AjaxHtmlForm.cs" /> <Compile Include="UI\Controls\ClientEventControls\BaseClientEventWrapper.cs" /> <Compile Include="UI\Controls\ClientEventControls\ClientEventControl.cs" /> Index: MagicAjaxModule.cs =================================================================== RCS file: /cvsroot/magicajax/magicajax/Core/MagicAjaxModule.cs,v retrieving revision 1.58 retrieving revision 1.59 diff -C2 -d -r1.58 -r1.59 *** MagicAjaxModule.cs 11 Jan 2006 20:27:32 -0000 1.58 --- MagicAjaxModule.cs 20 Jan 2006 01:35:56 -0000 1.59 *************** *** 27,30 **** --- 27,31 ---- using System.Web.UI; using System.Web.UI.WebControls; + using System.Web.UI.HtmlControls; using System.IO; using System.Text; *************** *** 33,36 **** --- 34,38 ---- using MagicAjax.Configuration; using MagicAjax.UI; + using MagicAjax.UI.Controls; namespace MagicAjax *************** *** 457,469 **** } // Continue only if it is a postback or an AjaxCall if ("GET" == _request.HttpMethod) return; - Page currentPage = HttpContext.Current.Handler as Page; - #if NET_2_0 ! ! if (currentPage != null) { // For ASP.NET 2.0, disable event validation on the page --- 459,479 ---- } + _magicAjaxContext.IsAjaxCall = (_request.Form["__AJAXCALL"] != null); + + Page currentPage = HttpContext.Current.Handler as Page; + + #if NET_2_0 + // On Page-init, we're going to replace the regular HTMLForm control with an AjaxHMTLForm + // For now, only supported for .NET 2.0 + if (currentPage != null) + currentPage.Init += new EventHandler(currentPage_Init); + #endif + // Continue only if it is a postback or an AjaxCall if ("GET" == _request.HttpMethod) return; #if NET_2_0 ! if (currentPage != null && _magicAjaxContext.IsAjaxCall) { // For ASP.NET 2.0, disable event validation on the page *************** *** 472,477 **** } #endif - - _magicAjaxContext.IsAjaxCall = (_request.Form["__AJAXCALL"] != null); string configState = _request.Form["__MAGICAJAX_CONFIG"]; if (configState != null) --- 482,485 ---- *************** *** 605,608 **** --- 613,659 ---- } + + /// <summary> + /// On Page init, we're going to we're going to replace + /// the regular HTMLForm control with our own AjaxHMTLForm. + /// This way we can implement a custom rendering of the form, + /// which reflects added scripts and hidden fields on Ajax callbacks + /// </summary> + /// <param name="sender">The current page</param> + /// <param name="e"></param> + void currentPage_Init(object sender, EventArgs e) + { + Page thePage = (Page)sender; + if (thePage != null) + { + HtmlForm oldFrm = (HtmlForm)Util.GetPrivateField(thePage, typeof(Page), "_form"); + AjaxHtmlForm newFrm = new AjaxHtmlForm(); + + // Copy all properties over + Util.ForceShallowCopyOnObject(newFrm, oldFrm); + + // when adding a control - the framework will try to disconnect it from its parent + // so we better make sure it it has no parent. + Util.SetPrivateField(newFrm, typeof(Control), "_parent", null); + Util.SetPrivateField(thePage, typeof(Page), "_form", newFrm); + + // Now replace the current Form control by our own + Control hostingControl = (Control)thePage; + #if NET_2_0 + if (thePage.Master != null) + { + MasterPage master = thePage.Master; + while (master.Master != null) + master = master.Master; + + hostingControl = (Control)master; + } + #endif + int formIndex = hostingControl.Controls.IndexOf(oldFrm); + hostingControl.Controls.RemoveAt(formIndex); + hostingControl.Controls.AddAt(formIndex, newFrm); + } + } + /// <summary> /// Handles the EndRequest event of the Application *************** *** 628,632 **** // If the ViewState wasn't excluded from the post data, retrieve // it and send it to client. ! if ( _magicAjaxContext.IsPageNoStoreMode && _request.Form["__VIEWSTATE"] != null ) { string vsValue = _filter.GetViewStateFieldValue(); --- 679,683 ---- // If the ViewState wasn't excluded from the post data, retrieve // it and send it to client. ! if (_magicAjaxContext.IsPageNoStoreMode && _request.Form["__VIEWSTATE"] != null) { string vsValue = _filter.GetViewStateFieldValue(); |