簡體   English   中英

如何在window.open函數ASP.NET中調用WebConfig appsetting值?

[英]How WebConfig appsetting value can be called inside a window.open function ASP.NET?

<input id="Button1" type="button" value="button" onclick='window.open("https://google.com")' />

我必須使用webconfig appsettings更改此設置。

在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值嗎?

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

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 %>")' />

使用JS方法:

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

和:

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

或者在JS中讀取密鑰

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM