Basically scripts inserted with UpdatePanel are never executed. They are just sitting there and doing nothing. You have to execute them after inserting into page.
I created class for my current project:
public static class UpdatePanelMethods
{
public static void AddUpdateScript(this UpdatePanel panel, string key, string script)
{
if (!panel.Page.ClientScript.IsClientScriptBlockRegistered(key))
{
ScriptManager.RegisterClientScriptBlock(
panel, typeof(UpdatePanel), key, script, true);
}
}
}
You are calling this like:
upSkorowidz.AddUpdateScript("upScript", "foo(bar)");
It's not ideal. It's string, so it better be really short and simple.
If you have to execute something much more complicated insert it like regular script but enclose this in some function:
<script>
foo(bar){
[whole script]
}
</script>
That way you can complex things in that script and you got intellisense.
What is downside?
You have to call this method (AddUpdateScript) in .cs code. JS scritp have to be more complicated since you modify this to simple call in string in .cs file.
If this reaaaally not the case for you, you can also use img.error event. It is possibly one of most ugly thing I wrote in my career :(
<%if (Page.IsPostBack)
{
<script type="text/javascript" id="someScript">
[some js]
</script>
<img src="dummyImg.jpg" onerror="eval(jQuery('#someScript').text())" alt"" width="0px" height="0px" />
<%} %>
What is happening?
There is not image dummyImg.jpg in server. So browser calls onerror event which executes our script. It is one of 2 events that can be executed by html injected in HTML after AJAX.
Second is frame.onload, but I did not want a frame in my html and did not tested it.
I read that also object.onload should work but I have found that untrue.
But it is very dirty solution and I would advice against it unlesss you want to end in DailyWTF :)