achmedmedusa
New Member
I have been on a journey the past few weeks, trying to figure out a multitude of defects in a pretty old application with lots of infrastructure. It uses multiple 3rd party controls that I cannot possibly hope to fix in the timeframe I have. One of these defects comes down to multiple javascript models of the client state. Specifically some controls expect to be able to hook into the jQuery form submit event, while others work in raw .NET (overriding \[code\]theForm.onsubmit\[/code\] directly1) and still others make use of \[code\]Sys.WebForms\[/code\] and register event handlers with \[code\]PageRequestManager\[/code\] (on some pages).At first I thought I could simply add a new \[code\]__doPostBack\[/code\] function to the page, but I am unable to inject it (sometimes) to run between the standard one and code that \[code\]Sys.WebForms\[/code\] runs to intercept \[code\]__doPostBack\[/code\]. If I override it afterwards then I either cannot trigger the logic internal to WebForms or cannot trigger the jQuery event. I am able to inject it before the original \[code\]__doPostBack\[/code\] but that does nothing without disabling the original function from being added to the page. So I came up with the following code. If this is the only way to do what I am actually attempting, why haven't I found it online already? Is there a better way to do this?\[code\]public class Form : System.Web.UI.HtmlControls.HtmlForm { const string DoPostBackFn = @" <script type=""text/javascript""> (function ($) { window.theForm = document.forms[0]; window.__doPostBack = function (eventTarget, eventArgument) { var originalvalues = [ theForm.__EVENTTARGET.value, theForm.__EVENTARGUMENT.value, theForm.onsubmit ]; if (!theForm.onsubmit || (theForm.onsubmit() != false)) { theForm.__EVENTTARGET.value = http://stackoverflow.com/questions/12700196/eventTarget; theForm.__EVENTARGUMENT.value = eventArgument; try { theForm.onsubmit = null; $(theForm).submit(); } finally { theForm.__EVENTTARGET.value = originalvalues[0]; theForm.__EVENTARGUMENT.value = originalvalues[1]; theForm.onsubmit = originalvalues[2]; } } }; }(jQuery)); </script>"; protected override void RenderChildren(HtmlTextWriter writer) { //temporarily disable the page from rendering the postback script var fRequirePostBackScript = typeof(System.Web.UI.Page).GetField("_fRequirePostBackScript", BindingFlags.Instance | BindingFlags.NonPublic); var isPostBackRequired = (bool)fRequirePostBackScript.GetValue(Page); if (isPostBackRequired) { fRequirePostBackScript.SetValue(Page, false); //write custom postback script writer.Write(DoPostBackFn); //tell the page that the script is rendered already typeof(System.Web.UI.Page).GetField("_fPostBackScriptRendered", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(Page, true); } //let ASP.NET do its thing base.RenderChildren(writer); //reset field to original value fRequirePostBackScript.SetValue(Page, isPostBackRequired); }}\[/code\]1 which is something you apparently cannot do when \[code\]Sys.WebForms\[/code\] is on the page because it blindly overwrites the DOM event without regard to what was already registered (at least in this version), so I'll have to do something about them elsewhere