ScriptManager.RegisterStartupScript doesn’t work

來源:互聯網
上載者:User

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

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.