2012-05-01 95 views
1

我正在一個web窗體站點上工作。頭標籤有runat服務器attr。在母版頁中。ResatClientUrl runat服務器頭內

我試圖使用ResolveClientUrl的.js文件是這樣的:

<head runat="server"> 
    .. 
    <script src='<%= ResolveClientUrl("~/myscript.js") %>' type="text/javascript" > </script> 
</head> 

但後來,我得到了一個錯誤:

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

我發現這個問題是RUNAT服務器,所以我從頭標記中刪除它...現在我得到以下錯誤:

Using themed css files requires a header control on the page. (e.g. <head runat="server" />). 

那麼,我該如何請在runat服務器頭中使用ResolveClientUrl?

UPDATE:

我添加使用包含在主網頁的頂部(似乎很好地工作)的腳本..但有可能是一個更好的解決方案。

<!-- #include file="myscript.js" --> 

回答

2

您可以使用數據綁定:

<head ID="head" runat="server"> 
    .. 
    <script src='<%# ResolveClientUrl("~/myscript.js") %>' type="text/javascript" > </script> 
</head> 

然後在你的代碼隱藏(最好在頁面加載事件處理程序):

head.DataBind() 
0

你得到消息的原因是因爲當引入代碼塊時,必須鎖定包含runat =「server」控件的ControlCollection,以防止出現意外情況,即核心框架在呈現時無法恢復(以及theref礦石執行渲染代碼塊)

使用任何代碼想要將「自己」添加到服務器控件集合中時,會變得更加複雜。

爲了防止這種情況,最好的辦法就是封裝任意代碼塊在文字控制在頭:

<head runat="server"> 
    <title></title> 
    <asp:Literal runat="server" id="SCRIPTLIT1"> 
    <script src='<%# ResolveClientUrl("~/myscript.js") %>' type="text/javascript"></script> 
    </asp:Literal> 

<!-- ...other stuff --> 
    <asp:ContentPlaceholder runat="server" id="head"></asp:ContentPlaceholder> 
</head> 

現在,我們已經推了一個框架,鎖定文字,而不是控制集合< head runat =「server」> control collection。