2014-03-06 73 views
0

我正尝试使用按钮在ASP.NET中的新选项卡中打开链接。我尝试以下,但它不工作:在ASP.NET的新选项卡中打开链接

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

在代码中,ReportButton_Click定义如下:

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

GoToPage定义如下:

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

您不能回发到服务器并打开新的选项卡/窗口。浏览器的弹出式窗口拦截器会阻止窗口。你想达到什么目的? – Win

+0

显示的错误是什么? –

+0

没有错误。 – muttley91

回答

2

不要做服务器端Response.Redirect,只是做一个客户端window.open。例如。

void GoToPage(string page) { 

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

} 

或更好 - 避免回发。您可以指定clientClick到您的按钮,如:

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

这样,新的页面将在客户端打开,而不需要返回到服务器。

+0

我应该把它放在哪里才能使它工作? – muttley91

+0

如果您使用第一个版本 - 它只是取代您的原始'GoToPage'功能。如果你第二次去 - 你必须把'LocationSkidPackReportPage'值改变的地方 –

0

LocationSkidPackReportPage一个公共属性在后面的代码,然后通过更换您的按钮:

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

,或者,如果您需要在代码中填补这一变种背后:

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

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

这个工作对我来说

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

你应该解释你在回答中发布的任何代码;这个想法是帮助提问者,以便他明白问题已经解决了,而不是简单地与他/她一起解决问题。 –

相关问题