简体   繁体   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")' />

I have to change this using webconfig appsettings. 我必须使用webconfig appsettings更改此设置。

In webconfig, I have 在webconfig中,我有

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

I have to get the url webconfig using the key. 我必须使用密钥获取url webconfig。

I have tried 我努力了

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

But it is not working. 但它没有用。

Could you find a solution to access the webconfig appsettings values in window.open function? 你能找到一个解决方案来访问window.open函数中的webconfig appsettings值吗?

Try using a variable in the code behind, for example 例如,尝试在后面的代码中使用变量

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

And then in the page 然后在页面中

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

EDIT - based on the comment of wanting to do it in the aspx page itself (not sure why you would want to do this, but I'm sure you have your reasons). 编辑 - 基于想要在aspx页面本身做的评论(不知道你为什么要这样做,但我确定你有你的理由)。

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

Use JS method: 使用JS方法:

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

and: 和:

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

Or read key in JS 或者在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