2017-02-23 15 views
1
<input id="Button1" type="button" value="button" onclick='window.open("https://google.com")' /> 

我必須改變這個使用webconfig appsettings。如何在window.open函數ASP.NET中調用WebConfig appsetting值?

在webconfig,我有

<add key="Google" value="https://google.com"/> 

我必須得到URL webconfig使用的關鍵。

我已經試過

<input id="Button1" type="button" value="button" onclick='window.open("<%= ConfigurationManager.AppSettings["Google"] %>")' /> 

但它無法正常工作。

你能找到一個解決方案來訪問window.open函數中的webconfig appsettings值嗎?

回答

0

使用JS方法:

function openUrl(url) { 
    var newWind = window.open(url, '_blank'); 
    newWind.focus(); 
} 

和:在JS

function openUrl() { 
     var url = '<%=ConfigurationManager.AppSettings["Google"].ToString() %>'; 
     var newWind = window.open(url, '_blank'); 
     newWind.focus(); 
} 
+0

它工作正常。謝謝您的幫助。 :)但附近的雙引號顯示錯誤消息「未終止的字符串常量」 –

0

嘗試在代碼中使用的變量後面,例如

string openUrl = ConfigurationManager.AppSettings["Google"]; 

然後在頁面

<input id="Button1" type="button" value="button" onclick='window.open("<%= openUrl %>")' /> 

編輯 - 基於想要做它在aspx頁面本身的評論(不知道你爲什麼想這樣做,但我相信你有你的理由)。

<% string openUrl = System.Configuration.ConfigurationManager.AppSettings["Google"]; %> 
<input id="Button1" type="button" value="button" onclick='window.open("<%= openUrl %>")' /> 
+0

<input id="Button1" type="button" value="button" onclick='openUrl("<%= ConfigurationManager.AppSettings["Google"].ToString() %>")' /> 

或閱讀鍵是否有aspx頁面本身的任何解決方案? –

+0

我想你可以用<% %>將代碼塊中的變量聲明包裝進去,如果你真的想沿着這條路走下去的話。看我的編輯。 –

+0

t工作正常。謝謝您的幫助。 :) –

相關問題