Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
603 views
in Technique[技术] by (71.8m points)

hyperlink - ASP.Net Open New Tab in Browser from CodeBehind

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...