Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I generate JavaScript during an AJAX callback (postback) and then execute it on the browser?
    text
    copied!<p><strong>In summary</strong>:</p> <p>I have an ASP.NET web page that causes an AJAX postback to the server. When this event handler runs (in the code behind) it will generate some JavaScript that I then want to run in the client. Not sure how to achieve this.</p> <p><strong>In Detail</strong>: I have an ASP.NET web page with multiple items displayed on the page.</p> <p>As a "nice to have", I want to display either a green circle or a red cross next to each item (these differ depending upon each item). Since it isn't vital for the User to see these icons and also because it takes several seconds to work out which icon should be shown for each item, I want to perform this after the page has loaded, so in an AJAX callback.</p> <p>My thought therefore was this. When creating the page, I would create both icons next to each object and create them with the style of "hidden". I would also make a note of each one's client ID.</p> <p>Then, when the callback occurs, I fetch the necessary data from the database and then create a JavaScript function that changes the display for each of the icons I want to show from "hidden" to "visible".</p> <p>I thought I could achieve this using the ScriptManager object.</p> <p>Here's a very trivial version of my server side code (C#)</p> <pre><code>void AjaxHandler(object sender, EventArgs e) { // call to database string jscript = "alert('wibble');"; ScriptManager.RegisterStartupScript(this, this.GetType(), "uniqueKey", jscript); } </code></pre> <p>Obviously, here I'm just trying to get an alert to fire after the postback has occurred...in real life I'd have my JavaScript function to change the display of all the icons I want to display.</p> <p>When I run this, the serverside code runs and yet nothing happens in the server.</p> <p>I have also tried:</p> <pre><code>ScriptManager.RegisterClientScriptBlock() Page.RegisterStartupScript() Page.RegisterClientScriptBlock() Page.ClientScript.RegisterStartupScript() Page.ClientScript.RegisterClientScriptBlock() </code></pre> <p>but none of them work....</p> <p>FireFox shows the following JavaScript error:</p> <blockquote> <p>Error: uncaught exception: [Exception... "Node cannot be inserted at the specified point in the hierarchy" code: "3" nsresult: "0x80530003 (NS_ERROR_DOM_HIERARCHY_REQUEST_ERR)" location: "http://localhost/MyWebSiteName/Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=ctl00_RadScriptManager1_TSM&amp;compress=1&amp;_TSM_CombinedScripts_=%3b%3bSystem.Web.Extensions%2c+Version%3d3.5.0.0%2c+Culture%3dneutral%2c+PublicKeyToken%3d31bf3856ad364e35%3aen-US%3a3de828f0-5e0d-4c7d-a36b-56a9773c0def%3aea597d4b%3ab25378d2%3bTelerik.Web.UI%2c+Version%3d2009.3.1314.20%2c+Culture%3dneutral%2c+PublicKeyToken%3d121fae78165ba3d4%3aen-US%3aec1048f9-7413-49ac-913a-b3b534cde186%3a16e4e7cd%3aed16cbdc%3a874f8ea2%3af7645509%3a24ee1bba%3a19620875%3a39040b5c%3af85f9819 Line: 1075"]</p> </blockquote> <p>Does anyone know if what I am trying to do is even allowed?</p> <p>If not - what's my alternative?</p> <p>Thank you</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