javascript 链接按钮的href url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10172567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:54:40  来源:igfitidea点击:

Linkbutton href url

c#javascriptasp.netlinkbutton

提问by nick gowdy

I have a loop in my code with dynamic linkbuttons and I want to set the OnClientClick property to open up the link in a new browser window. It sounds simple but I am having a bit of trouble getting it to work.

我的代码中有一个带有动态链接按钮的循环,我想设置 OnClientClick 属性以在新的浏览器窗口中打开链接。这听起来很简单,但我在让它工作时遇到了一些麻烦。

lbUrl = new LinkButton();
lbUrl.Text = r["Url"].ToString();

// r["Url"] is Datarow
lbUrl.OnClientClick = "location.href=" + r["Url"].ToString();

Any ideas?

有任何想法吗?

回答by Curt

window.open()will open the link in a new browser tab/window

window.open()将在新的浏览器选项卡/窗口中打开链接

lbUrl.OnClientClick = String.Format("window.open('{0}')", r["Url"]);

Ensure your URL is an Absolute URL

确保您的 URL 是绝对 URL