简体   繁体   中英

Open Link in New Tab in ASP.NET

I'm trying to use a Button to open a link in a new tab in ASP.NET. I'm trying the following but it isn't working:

<asp:Button ID="ReportButton" runat="server" CssClass="button" Font-Size="XX-Large" ForeColor="White" Text="Report" OnClick="ReportButton_Click" OnClientClick="form1.target='_blank';" /> 

In the code, ReportButton_Click is defined as follows:

protected void SkidPackReportButton_Click(object sender, EventArgs e)
{
    GoToPage(LocationSkidPackReportPage);
}

and GoToPage is defined as follows:

bool GoToPage(string page)
{
    try
    {
        Response.Redirect(page);
        return true;
    }
    catch (Exception)
    {
        StatusLabel.Text = "There was an error finding the page.";
        return false;
    }
}

Don't do server-side Response.Redirect , just do a client-side window.open . Eg

void GoToPage(string page) {

  ScriptManager.RegisterStartUpScript(this, this.GetType(), "newPage", String.Format("window.open({0});", page), True);

}

Or better yet - avoid postback altogether. You can assign clientClick to your button like:

ReportButton.OnClientClick = String.Format("window.open({0});return false;", LocationSkidPackReportPage);

This way new page will be opened on client without need to go back to the server.

Make LocationSkidPackReportPage a public property in code behind and then replace your button by:

<a href="<%=LocationSkidPackReportPage%>" class="button" target="_blank">Report</a>

OR, if you need to fill this var in code behind:

// Response.Redirect(page); -> Replace this by:

string script = String.Format("window.open('{0}', '_blank');", LocationSkidPackReportPage);
ScriptManager.RegisterStartUpScript(this, this.GetType(), "reportResultPage", script, True);

this work for me

  Page.ClientScript.RegisterStartupScript(
   this.GetType(), "OpenWindow", "window.open('../_Reportes/ReporteGeneral.aspx','_newtab');", true);

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