2011-04-18 70 views
3

我似乎無法將一個configurationmanager.appsettings變量打印到一個aspx頁面。所以在我的index.aspx文件中,我有以下一行代碼:在aspx頁面上打印一個configurationmanager.appsettings變量?

<%# (string) ConfigurationManager.AppSettings["myvariable"] %> 

但是沒有打印出來。

不過,如果我添加一個asp:textboxindex.aspx頁面,則變量從index.aspx.cs頁面綁定到它,像這樣:

textbox.Text = (string) ConfigurationManager.AppSettings["myvariable"]; 

然後,它顯示出來。

如何在沒有代碼的情況下將配置變量直接打印到index.aspx?

回答

5

<%#是一個數據綁定表達式,它僅在您執行數據綁定時進行評估。如果否,則不會呈現任何內容。你應該使用一個<%=結構來代替:

<%= ConfigurationManager.AppSettings["myvariable"] %> 

或者你可以使用甚至更短的<%$構建這裏例如爲:

<asp:Literal runat="server" Text="<%$ appSettings:myvariable%>" />