Combining RegisterClientScriptBlock Alongside add_endRequest
<p>Upon rendering, my page needs to initialize a client-side object which uses a custom <code>JavaScript</code> <code>'validator'</code> object.  Upon post-back, a separate object may get created that also uses the same custom <code>JavaScript</code> <code>'validator'</code> object.  Used in separation, both objects work well.  Once combined on a single page ONLY one-or-the-other ends-up working.</p>

<p>The objective is to let the client-side object work (as normal), but when a problem is encountered on any server-side event...pass-in any server-side messages to a separate instance of the 'validator' without affecting any other <code>JavaScript</code> objects in the page.</p>

<p><strong>The Problem Is:</strong><br/>
I can only get ONE OR THE OTHER to work...not BOTH at the same time!  And I need both to work.</p>

<p><strong>The Client-Side Code Looks Like:</strong><br/>
The problem starts occuring once I begin to use an update panel:</p>

<pre><code>&lt;script type="text/javascript"&gt;
&lt;!--
    var myCustomObject = (function ($) {
       // Nothing In Here Is Causing Any Issues...So Please Ignore
    })(jQuery);

    var instance = null;
    function initializePage()
    {
         instance = new myCustomObject.Form();
    }

    $(document).ready(function () {
         initializePage();

         // Using this alongside RegisterClientScriptBlock causes the issues
         if (typeof (Sys) != "undefined")
              Sys.WebForms.PageRequestManager.getInstance().add_endRequest(initializePage);
    });
--&gt;
&lt;/script&gt;
</code></pre>

<p><strong>The Server-Side Code Looks Like:</strong><br/></p>

<pre><code>protected void btnPostBack_Click(object sender, EventArgs e)
        {
            ValidationResult validator = Validate();
            if (!validator.IsValid)
                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "validationController.Registration", validator.ToHtml(divArea.ClientID), true);

            // I tried this and it fails too?
            //ScriptManager.GetCurrent(Page).RegisterDataItem(Page, "javascript:alert('hello world!');");
        }
</code></pre>

<p><strong>FireBug's Script Tab Show The Following Script Registered:</strong><br/>
All other aspects of the call run...except for the call to <code>validationController.displayAll()</code>.</p>

<pre><code>|scriptBlock|ScriptContentNoTags|
validationController = new Validation.Controller(jQuery('#ctl00_ContentPlaceHolder1_divArea'));
validationController.getValidator('ctl00_ContentPlaceHolder1_txtFirstName').appendMessage('Please enter a value.');
validationController.displayAll();
|
</code></pre>

<p><strong>Here Is What Firebug Shows:</strong><br/></p>

<ul>
<li>The page runs <code>initializePage</code> (my client-side-test button displays correct test-results when pressed)</li>
<li>The <code>console</code> says the <code>validationController</code> I added using <code>RegisterClientScriptBlock</code> exists</li>
<li><code>validationController.displayAll()</code> is listed in the <code>scriptBlock</code>...but NEVER RAN!</li>
<li>If I run <code>validationController.displayAll()</code> in the <code>console</code> it WORKS!</li>
</ul>

<p><strong>The Entire Page Looks Like:</strong><br/></p>

<pre><code>&lt;asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"&gt;

    &lt;script src="Scripts/jQuery/Core/jquery-1.6.2.min.js" type="text/javascript"&gt;&lt;/script&gt;
    &lt;script src="Scripts/jQuery/Core/jquery.tmpl.js" type="text/javascript"&gt;&lt;/script&gt;

    &lt;script type="text/javascript"&gt;
    &lt;!--
        var myCustomObject = (function ($) {
            // Nothing In Here Is Causing Any Issues...So Please Ignore
        })(jQuery);

        var instance = null;
        function initializePage()
        {
            instance = new myCustomObject.Form();
        }

        $(document).ready(function () {
            initializePage();

            // This causes the failure
            if (typeof (Sys) != "undefined")
                Sys.WebForms.PageRequestManager.getInstance().add_endRequest(initializePage);
        });
    --&gt;
    &lt;/script&gt;

&lt;/asp:Content&gt;
&lt;asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"&gt;

    &lt;asp:UpdatePanel ID="udPanel" runat="server" UpdateMode="Always"&gt;
        &lt;ContentTemplate&gt;

            &lt;div id="divArea" runat="server" style="position: absolute; top: 100px; left: 100px;"&gt;
                &lt;span style="display: block;"&gt;First Name&lt;/span&gt;
                &lt;input type="text" id="txtFirstName" runat="server" /&gt;
            &lt;/div&gt;

            &lt;input type="button" id="btnSaveCientSide" value="Run It Client-Side" /&gt;
            &lt;asp:Button ID="btnPostBack" runat="server" Text="Run It Server-Side" /&gt;

        &lt;/ContentTemplate&gt;
    &lt;/asp:UpdatePanel&gt;

&lt;/asp:Content&gt;
</code></pre>
