2013-07-26 38 views
0

我試圖從我的web配置文件中檢索一個路徑字符串,並使用它加上文件名來獲取我需要的圖像的url。我認爲它應該是這樣的:來自web.config的ASP字符串連接在img src

<asp:Image ImageUrl ='<%# System.Configuration.ConfigurationManager.AppSettings["AppPath"] + "&Images/headerbk01.jpg"%>' runat ="server" width="983" height="265" /> 

這給我一個空的字符串的來源。

編輯:

現在使用此:

<asp:Image ImageUrl ='<%=System.Configuration.ConfigurationManager.AppSettings["AppPath"]+"&Images/headerbk01.jpg"%>' runat ="server" width="983" height="265" /> 

產生以下爲html:

<img style="width: 983px; height: 265px;" src="<=System.Configuration.ConfigurationManager.AppSettings["AppPath"]+"&Images/headerbk01.jpg"%>"/> 
+0

你能嘗試的=而不是代碼中的#號?這可能會有所作爲。 –

+0

謝謝,剛剛嘗試過,沒有改變,我的src直到一個空字符串 – collusionbdbh

+0

我試過了字符串temp = System.Configuration.ConfigurationManager.AppSettings [「AppPath」];在頁面後面的代碼中,它正在拾取正確的值 – collusionbdbh

回答

0

您需要將數據綁定到圖像控制。一個ID添加到圖像:

<asp:Image ID="Image1" ImageUrl ='<%# System.Configuration.ConfigurationManager.AppSettings["AppPath"] + "&Images/headerbk01.jpg"%>' runat ="server" width="983" height="265" /> 

後面的代碼中添加以下代碼:

Image1.DataBind(); 

或者,如果它的好與IMG,試試這個:

<img src='<%=System.Configuration.ConfigurationManager.AppSettings["AppPath"]+"&Images/headerbk01.jpg"%>' style="width:983px; height:265px;" /> 
+0

這沒有幫助,問題在於代碼實際上並未從Web配置中獲取值,而src只是被設置爲代碼的實際字符。 – collusionbdbh

+0

你確定Image1.DataBind();執行? – afzalulh

+0

第二種解決方案似乎有效。感謝大家。 – collusionbdbh

2

事情是這樣的:

C#

<asp:Image ImageUrl='<%# System.ConfigurationManager.AppSettings["yoursetting"] + "&Images/headerbk01.jpg" %>' runat = "server" Width="983" height="265" alt="image1.jpg" /> 

VB.NET

<asp:Image ImageUrl='<%# System.ConfigurationManager.AppSettings("yoursetting") & "&Images/headerbk01.jpg" %>' runat = "server" Width="983" height="265" alt="image1.jpg" /> 
+0

它看起來可能是正確的,但我只是得到:名稱空間「系統」中不存在類型或名稱空間名稱「ConfigurationManager」(是否缺少程序集引用?)在我的asp頁面中。難道我需要在asp頁面中添加一個程序集引用嗎? – collusionbdbh

+0

好了解決了這個問題System.Configuration.ConfirgurationManager.AppSettings – collusionbdbh

+0

問題回答我猜 –