2010-12-10 111 views
4

我有一個棘手的情況,由此我想只有在某些條件下添加的ScriptManager的ScriptReference如下將腳本添加到ScriptManager的條件

<asp:ScriptManagerProxy ID="scriptManagerProxy1" runat="server"> 
<CompositeScript> 
    <Scripts> 
     <asp:ScriptReference path=/...." /> 
    </Scripts> 
</CompositeScript> 
<asp:ScriptManagerProxy> 

我想要只在特定條件下這個腳本參考,所以我也爲下面

<% if(xyzclass.property) 
{ %> 

above code 

<% } %> 

一旦我這樣做,我得到的錯誤作爲

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>). 

我用Google搜索並嘗試添加「#」作爲<%#,但通過添加「#」無法找到類(xyzclass)等得到的錯誤作爲

Expected class, delegate, enum, interface, or struct 

我試圖做的工作爲這裏也提到 http://leedumond.com/blog/the-controls-collection-cannot-be-modified-because-the-control-contains-code-blocks/

到目前爲止沒有運氣。如果我採取的辦法是在上面的鏈接提到的,說像

The base class includes the field '', but its type (System.Web.UI.ScriptManagerProxy) is not compatible with the type of control (System.Web.UI.ScriptManager). 

夥計們,我需要的是剛剛通過的ScriptManager ONLY動態添加腳本。有沒有什麼方法在實踐中也是好的。

由於提前,

的NiMesh

回答

7

如果你想添加基於條件的腳本,可編程添加它們:

ScriptManager mgr = ScriptManager.GetCurrent(this.Page); 
if (condition) 
    mgr.Scripts.Add(new ScriptReference { Path = "~/script.js" }); 
代碼

後面。或者,使用ScriptManagerProxy並在用戶控件或頁面本身中定義它們。這是添加腳本的好方法,但是如果您使用複合腳本,它會將這些腳本追加到與ScriptManager相同的複合腳本中。

HTH。

+0

嗨,夥伴,已經做到了,但不起作用。當我做ScriptManger1.CompositeScript.Scripts.Add(item)時,不起作用。腳本未加載到網頁中。使用Firebug查看ScriptReference中是否存在腳本。 – DotNetInfo 2010-12-10 03:04:11