Writing single client script block for mutiple controls


Jason Barnett

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?



Nathan Sokalski

In the RegisterClientScriptBlock method, what are you using as the key (the
first parameter)?

Jason Barnett

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

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

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

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




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.