简体   繁体   中英

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.

I am then creating clickable buttons in a table which need to open a new window and launch those URLs. These are held in object's String variables.

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.

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()

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. 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.

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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