最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Linkbutton href url - Stack Overflow

programmeradmin1浏览0评论

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.

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

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

Any ideas?

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.

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

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

Any ideas?

Share Improve this question edited Apr 16, 2012 at 10:43 Curtis 103k69 gold badges277 silver badges359 bronze badges asked Apr 16, 2012 at 10:39 nick gowdynick gowdy 6,53127 gold badges95 silver badges158 bronze badges 2
  • you .ToString in the end is redundant – Rune FS Commented Apr 16, 2012 at 10:43
  • Your code changes the page displayed at the same window, try @Curt's answer, or try this, to open URL at same page : lbUrl.OnClientClick = "location.href='" + r["Url"].ToString() + "'"; – Canavar Commented Apr 16, 2012 at 10:45
Add a ment  | 

1 Answer 1

Reset to default 3

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

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

Ensure your URL is an Absolute URL

发布评论

评论列表(0)

  1. 暂无评论