ASP.Net Open New Tab in Browser from CodeBehind
Asked Answered
F

2

15

I need to open a browser tab from a link that is given to me by an asp.net code behind. Normally I would have a link and target="_blank", but the link that I need is dynamic, so I must have the behavior of a _blank link from code behind.

Any Ideas?

Festival answered 3/4, 2011 at 14:47 Comment(0)
T
33

If you have the data needed to create the link when generating the initial HTML, you can do something like this in the Page_Load event:

protected void Page_Load(object sender, EventArgs e)
{
    Button1.OnClientClick="javascript:window.open('MyPage.aspx?Param=" + Param1.ToString() + "');";         }
}

If you're waiting for the PostBack to get the required data to build the link, you can send javascript down to the browser via the ScriptManager:

protected void Button1_Click(object sender, EventArgs e)
{
    //process whatever you need to to get Param1
    ScriptManager.RegisterStartupScript(Page, typeof(Page), "OpenWindow", "window.open('MyPage.aspx?Param=" + Param1.ToString() + "');",true);
}
Thekla answered 3/4, 2011 at 15:2 Comment(2)
hi james what if I want to open this in new tab ? Please helpHarbinger
Saved my day. Thanks!Gabriellagabrielle
I
0

You're looking for the Target property.

Iconostasis answered 3/4, 2011 at 14:49 Comment(2)
No, you missunderstood me... I don't have a link. I have a normal ASP:Button and in CodeBehind I have a lot of ToDos (DB Query and so on). After finish I must open a link in a new tab.Festival
Then you need to emit a Javascript call to window.openIconostasis

© 2022 - 2024 — McMap. All rights reserved.