這個問題,對我來說,確實一個相當鬱悶的問題,耗費了幾乎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.