ScriptManager.RegisterStartupScript doesn't work

这个问题,对我来说,确实一个相当郁闷的问题,耗费了几乎2个小时才搞定。

先看看RegisterStartupScript 的参数签名:

RegisterStartupScript(control, type, key, script, addScriptTags)

RegisterStartupScript(page, type, key, script, addScriptTags)

 

在我个人的印象中,this跟当前的Page对象是一样的,不过确实不一样。

看看上面的两个重载函数,

当我们使用第一个方法的时候,无论如何,添加的代码都不执行。

但是用第二个方法的时候,就没有问题了。

参加国外牛人的解释:

---

I could resolve this issue and just thought of updating here if anyone else face it sometime. The problem is this. If you use ScriptManager.RegisterStartupScript and none of the update panels get updated at client due to  UpdateMode="Conditional" and ChildrenAsTriggers="False" , this method wont render any script. And this is weird because as per the ajax documentation, RegisterStartupScript Method (page, type, key, script, addScriptTags) will causes the script to be included each time an asynchronous postback occurs.

If you didn't notice it already, RegisterStartupScript method has two overloaded instances. One takes Control and other Page as the first arguments (rest of the arguments are common). And if you use the one which takes page as argument, its suppose to render the script everytime which id doesn't. This is apparently a bug in asp.net ajax.

I reflected these methods using Lutz Roeder's .Net Reflector (amazing tool by the way if you haven't used already) and found that both of these methods call ScriptRegistrationManager.RegisterStartupScript(control, type, key, script, addScriptTags). I don't understand how calling a same method with same code from two different methods makes any difference. So it seems that RegisterStartupScript Method (page, type, key, script, addScriptTags) method is duplicate and just useless.

Anand

---

 

原来这是asp.net ajax的一个bug。

this demo doesn't work:

scripts = "alert(123);";
ScriptManager.RegisterStartupScript(
this, this.GetType(), "showWindow", scripts, true);

 

this demo is working:

scripts = "alert(123);";
ScriptManager.RegisterStartupScript(Page, Page.GetType(),
"showWindow", scripts, true);

 

over.

 

 

posted @ 2010-03-22 16:12  无尽思绪  阅读(737)  评论(4编辑  收藏  举报