2011-07-15 42 views
1

以下代碼:asp.net數據綁定表達不評估

aspx文件:

<div id="challengedIndependence" class="collegeAccentBlock grid_8 push_1" runat="server" clientidmode="Static" style='display:<%# testMethod %>' > 

相關aspx.cs文件:

public string testMethod() 
    { 
     return "none;"; 
    } 

呈現的HTML文件:

<div id="challengedIndependence" class="collegeAccentBlock grid_8 push_1" style="display:&lt;%# testMethod %&gt;"> 
</div> 

我在等待結果ndder as ...風格:「display:none;」...我做錯了什麼。

不知道這是否應該在同一個問題:我這樣做是因爲我有多行代碼來確定是否應顯示此div。如果還有另一種方式來做這件事,並解釋爲什麼它更好,我也會接受這個答案。

參考:Inline expressions referenced on MSDN

+1

你在哪裏進行數據綁定? – Oded

+0

它可以用屬性而不是方法工作嗎? – sll

+0

在Oded的評論後,我改變了代碼,使用顯示錶達式指令'<%= %>',這沒有奏效,我不確定原因。數據綁定發生爲file.aspx.cs for file.aspx – sammarcow

回答

1

如果你打算在這裏使用的內嵌表達式,它需要的是屬性的全部內容,就像這樣:

<div id="challengedIndependence" class="collegeAccentBlock grid_8 push_1" runat="server" clientidmode="Static" style='<%# testMethod() %>' > 

,然後返回整個字符串你想在那裏。它只是將它渲染爲字符串的一部分。

+0

這是正確的。 – sammarcow

0

通過代碼聽寫的UI風格背後看起來並無大礙,但無論如何試圖返回的整體風格字符串如return "display:none"並綁定到樣式屬性