Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You said your initial goal was:</p> <blockquote> <p>The idea was that the page would load, data would be sent (AJAX) to the server. The server would then generate some JavaScript based upon this data and send that back to the page. That JavaScript would then run updating the page in a specific way.</p> </blockquote> <p>Here's a way you can do that:</p> <p>given:</p> <pre><code>&lt;asp:ScriptManager runat="server" ID="scriptManager"&gt; &lt;/asp:ScriptManager&gt; &lt;script type="text/javascript"&gt; function endRequestHandler(sender, args) { var dataItems = args.get_dataItems(); for(var key in dataItems){ if(/^javascript:/.test(dataItems[key])){ eval(dataItems[key].substring("javascript:".length)); } } } Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler); &lt;/script&gt; &lt;asp:UpdatePanel runat="server" ID="pnl"&gt; &lt;ContentTemplate&gt; &lt;asp:Button runat="server" ID="btnClick" Text="Click me!" OnClick="btnClick_Click" /&gt; &lt;/ContentTemplate&gt; &lt;/asp:UpdatePanel&gt; </code></pre> <p>You can create a click handler that does this:</p> <pre><code>protected void btnClick_Click(object sender, EventArgs e) { ScriptManager.GetCurrent(Page).RegisterDataItem(this, "javascript:alert('hello world!');"); } </code></pre> <p>What's happening is during the postback, the page request manager is sent a data item your code-behind. That data-item happens to be a javascript command. After the postback, the client side script manager's endRequest handler is checking for data items. Normally you'd want to see who those items are for, which is apparent by the key of the item (it's the client ID of the control that is the target of the data being sent). In your case, you could load this up with the javascript that you want to fire, tell yourself that it's a javascript because it's prepended, then dynamically evaluate the script.</p> <p>So in this example, clicking the "Click Me!" button will generate a Hello World prompt whose script was actually created by the code-behind during the postback.</p> <p>You'll have to be very cautious with this approach until you're comfy - I'd avoid references to "this"...</p> <p>Happy coding.</p> <p>B</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload