2011-05-04 65 views
0

我在的.cs頁面使用這個腳本來控制..問題添加JavaScript

public void messagebox(string msg) 
{ 
    Label lbl = new Label(); 
    lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>"; 
    Page.Controls.Add(lbl); 
} 

誤差

Controls集合不能修改,因爲該控件包含代碼塊(即<%.. 。%>)。

+2

你應該在你之前的問題上接受更多答案你繼續問更多。 – 2011-05-04 13:42:09

+1

最初我看了很多未回答的問題,但是之後出現了24個問題,很多人都回答了問題,至少有些問題值得接受 – mplungjan 2011-05-04 13:46:01

+0

[ASP.NET中的JavaScript警報]的可能重複(http://stackoverflow.com /問題/ 5713000/JavaScript的警報功能於ASP網) – 2011-05-04 13:54:45

回答

2

要註冊一個腳本,使用ScriptManager的

ScriptManager.RegisterStartupScript(this, this.GetType(), "registeredAlert", lbl, false); 
0

這與Javascript無關。錯誤信息非常清楚;你不允許在你的代碼中的那一點上添加任何更多的控件到Page.Controls。

1

要在頁面上使用RegisterStartupScript

ScriptManager.RegisterStartupScript Method (Control, Type, String, String, Boolean) 

註冊一個腳本,如果您使用的是更新面板,您必須調用前兩個參數與更新面板控件

ScriptManager.RegisterStartupScript(updatePanel, 
    updatePanel.GetType(), 
    "key",   // unique key means it will never insert the same script twice 
    "alert('hi');", // javascript 
    true);   // include <script> tags