Saturday, March 10, 2012

user control with script blocks not loading in UpdatePanel

This is not a big with the Ajax library. You can call this a browser bug. The UpdatePanel works by setting the innerHTML property of an HTML div element. If you are assigning a string to the innerHTML property it cannot contain script blocks. So your inline script does not work. There are several solutions. The easiest solution is to avoid in-line script and use ScriptManager.RegisterClientScriptBlock method.

Ok, that makes sense but is there a better solution than RegisterClientScriptBlock() which has some draw backs such as: no intelisense; hard to read large scripts when it is built by puting together strings; doesn's work with break points; etc.

And a second problem that may be related to this. For some reason when I use RegisterClientScriptBlock() in the OnPreRender of a user control, again the script is not registered with partial postbacks but registers well on normal postbacks.


On your recommendation I tried this:

ScriptManager.RegisterClientScriptInclude(Me,Me.GetType,"StaticScript", ResolveClientUrl("~/ProjectLineItems.js"))

And now I get this error:

"Microsoft JScript runtime error: Sys.ScriptLoadFailedException: The script 'ProjectLineItems.js' failed to load. Check for:
Inaccessible path.
Script errors. (IE) Enable 'Display a notification about every script error' under advanced settings.
Missing call to Sys.Application.notifyScriptLoaded()."

The path is fine and I even see the <script> tag that the framework rendered on the page.


My guess is that the last suggestion in the error message is correct. Make sure your script ends with this line: "Sys.Application.notifyScriptLoaded();"


Oh, thanks. I was not aware of this procedure.

One last thing. When I was using inline script, I could inject some dynamic script by using <% =SomeFunc() %> in the javascript. How can this be done with RegisterClientScriptInclude() or any other function short of having the script in the code behind.


I'd say put your script in the code behind, but you told me not to. :-)

The only alternative that immediately comes to mind is to break your script into two pieces... put all the static script in a .js file, and then put your dynamic script (the part that needs SomeFunc(), for example) in the codebehind as a script block you register from code. Then you can use something like string.Format("...{0}...", SomeFunc()). Basically, you could put a small piece of your script in the codebehind instead of your entire file.

Sorry I can't come up with anything better.


Yep, that is the solution I came up with too.

One more thing if you don't mind. How do I register something like this "<link href='ClickMenu.css' type='text/css' rel='stylesheet'>" with a partial postback. RegisterClientScriptBlock complains when I do this:

ScriptManager.RegisterClientScriptBlock(Me, Me.Page.GetType, "ArgoClickMenuCss", "<link href='ClickMenu.css' type='text/css' rel='stylesheet'>", False)

I get an error:

The script tag registered for type 'ASP.projectviewer_asp' and key 'ArgoClickMenuCss' has invalid characters outside of the script tags <link href='ClickMenu.css' type='text/css' rel='stylesheet'>. Only properly formatted script tags can be registered.

Don't know what this means. This worked before with the Page.ClientScript.RegisterClientScriptBlock but not with the new Ajax ScriptManager.RegisterClientScriptBlock


I had the same error as your previous post. I guess the script manager only wants to add javascript in the client script block. However, I found another method that will register any string (at least my string, which was a style tag).

1if(ScriptManager1.IsInAsyncPostBack)2 ScriptManager1.RegisterDataItem(this, anystring);3else4 ScriptManager.RegisterClientScriptBlock(this,typeof(string),"reportStyles", anystring,false);5

No comments:

Post a Comment