2008-09-16 59 views
3

基本上我想找到一種方法,DDO是這樣的:如何將一個標籤的標記「綁定」 Text屬性

<asp:Label ID="lID" runat="server" AssociatedControlID="txtId" Text="<%# MyProperty %>"></asp:Label> 

我知道我可以從代碼中設置它的背後(寫入lId.Text = MyProperty),但我更喜歡在標記中做,而我似乎無法找到解決方案。 (myProperty的是一個字符串屬性) 歡呼

回答

0

呼叫lID.Databind()的代碼隱藏

2

保留標記爲是做出的Page.DataBind()的調用;在你的代碼背後。

2
<asp:Label id="lID" runat="server"><%= MyProperty %></asp:Label> 

因爲asp.net標籤不允許<%%>構造,您不能使用文本= 「<%= myProperty的%>」。

+0

謝謝,我已經走了這個解決方案,但即使調用的Page.DataBind( )工作得很好。 – 2008-09-16 15:34:43

0
<div> <%=MyProperty"%></div> 
+0

標籤控件甚至不會渲染div標籤。如果你建議,這可能是有意義的,但即使這樣也不會有相同的效果,因爲你避免了標籤的所有屬性,並且不允許使用主題。 – 2008-09-16 15:33:55

+0

是的,在我的情況下,我不需要div,因爲我使用控件的AssociatedControlId屬性來爲文本框創建標籤。 – 2008-09-16 15:37:08

8

你可以做

<asp:Label runat="server" Text='<%# MyProperty %>' /> 

然後一個的Page.DataBind()的代碼隱藏。

+0

您遺漏了左邊的%...應該是: `Text ='<%#MyProperty%>'' 無論如何,謝謝。你有唯一答案是99%正確的。 :) 也值得注意,你可以這樣做: `Text ='<%#myClass.MyProperty%>'' – maplemale 2014-08-25 00:44:29

0

當您使用<%#%myProperty的>聲明你需要數據綁定,但使用<%= myProperty的%>你沒有(這類似於只是寫的Response.Write(myProperty的)。

0

你可以這樣做:

<asp:Label ID="lblCurrentTime" runat="server"> 
    Last update: <%=DateTime.Now.ToString()%> 
</asp:Label> 
相關問題