Writing single client script block for mutiple controls

Discussion in 'ASP .Net Building Controls' started by Jason Barnett, Jun 9, 2006.

  1. I'm using .NET 1.1 and I've created a server control that writes javascript
    to the output writer. The control works great, except when I add additional
    controls. The problem is that each control writes the same javascript block,
    which causes a stack overflow when it is executed.

    Could someone tell me how I may write the javascript to the page only once,
    regardless of how many controls are dropped on the web form?
    Jason Barnett, Jun 9, 2006
    1. Advertisements

  2. In the RegisterClientScriptBlock method, what are you using as the key (the
    first parameter)?
    Nathan Sokalski, Jun 9, 2006
    1. Advertisements

  3. I'm not using the RegisterClientScriptBlock method. From what I've read,
    that is a method on the ClientScriptManager class and is only available in
    ..NET 2 or higher. Is there something comparable in .NET 1.1?

    I'm currently writing the script as plain text using output.Write method
    within the Render method.
    Jason Barnett, Jun 9, 2006
  4. Ok, I now see where I made one mistake. I did a search on
    RegisterClientScriptBlock and found that it is a method on the Page class
    (although, it does not appear in intellisense). I modified my code to use
    the RegisterClientScriptBlock method in conjunction to the
    IsClientScriptBlockRegistered method, but multiple blocks are still being
    written to the page.

    I'm using a constant as the key (ex: "MyClientScript").
    Jason Barnett, Jun 9, 2006
  5. I am starting to wonder if the problem lies within the script block itself...
    here is an example of what I'd like written out only once. Does it matter
    that it is linked to an external .js file?

    <script src="javascrip/myControl.js" type="text/javascript"></script>
    Jason Barnett, Jun 9, 2006
  6. Ok, I got it. I was trying to use the RegisterClientScriptBlock method
    during the Render event. When I used it during the PreRender event it began
    Jason Barnett, Jun 9, 2006
  7. Jason Barnett

    sam Guest

    Watch out for this method. After 8 different calls it will start
    outputing the script tags in a random order with respect to what order
    the method executes in the page. this is because microsoft uses the
    retarded HybridDictionary class.

    Should'nt make a difference unless you require the script tags to be
    output in a specific order.
    sam, Jun 17, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.