简体   繁体   English

通过C#和ASP.Net从JSON打开URL

[英]Open URL from JSON via C# and ASP.Net

I have variable URL strings that are read from an external JSON block into the C# code behind. 我有可变的URL字符串,从外部JSON块读取到C#代码后面。

I am then creating clickable buttons in a table which need to open a new window and launch those URLs. 然后我在表格中创建可点击按钮,需要打开一个新窗口并启动这些URL。 These are held in object's String variables. 它们保存在object的String变量中。

However, I cannot find a way to make a function on the aspx side that opens a window on click and uses the URL string. 但是,我找不到在aspx端创建一个函数的方法,它在click上打开一个窗口并使用URL字符串。

Currently I am adding a attribute to the button 目前我正在为按钮添加一个属性

Button b = new Button();
b.Attributes.Add("onClick", "OpenURL()");
bCell.Controls.Add(b); 

With this I can open a window, but I can't seem to get the URL I deserialized from the JSON string over to the OpenURL() 有了这个,我可以打开一个窗口,但我似乎无法将我从JSON字符串反序列化的URL转换为OpenURL()

function OpenURL(url) {var x = window.open(url, 'mynewwin');

function on the front end. 功能在前端。

Since the url varies, I cannot hard code it anywhere. 由于网址不同,我无法在任何地方进行硬编码。

All of the buttons, rows, and cells are generated dynamically from the JSON strings. 所有按钮,行和单元格都是从JSON字符串动态生成的。 So no hard coding can happen on these. 因此,这些都不会发生硬编码。 //First time poster. //第一次发布海报 Tried to look for solutions but failed 试图寻找解决方案,但失败了

If you know what the url is at the time you're creating the button, you can do: 如果您在创建按钮时知道网址是什么,则可以执行以下操作:

Button b = new Button();
var url = "some url";
b.Attributes.Add("onClick", string.Format("OpenURL({0})",url));
bCell.Controls.Add(b); 

If you don't know the url until after the page is loaded, you can store it in a variable on the page and retrieve it when you click the link. 如果在加载页面之前您不知道URL,则可以将其存储在页面上的变量中,并在单击链接时检索它。

<script>
    var url;

    //have whatever you use to set the url call this function
    function setUrl(inputUrl){
        url = inputUrl; 
    }

    function OpenURL(){
        var x = window.open(url,'some window');
    }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM