簡體   English   中英

在ASP.NET的“新建”選項卡中打開鏈接

[英]Open Link in New Tab in ASP.NET

我正在嘗試使用Button在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;
    }
}

不要執行服務器端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);

這樣,新頁面將在客戶端上打開,而無需返回到服務器。

在后面的代碼中將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);

這項工作對我來說

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM