簡體   English   中英

使用 ASP.NET 按鈕、C# 和頁面背后的代碼在新選項卡中打開窗口

[英]Open Window in new tab using an ASP.NET Button, C#, and the code behind the page

我正在一個生成內部報告的網站上工作,我的任務是在新選項卡/窗口中打開這些報告,但必須限制任何腳本必須從背后的代碼運行,而不是寫在網站的 html/aspx 部分.

我目前測試代碼的嘗試是這樣的:

 protected void Button2_Click(object sender, EventArgs e)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "OpenWindow", "window.open('http://www.google.com','_newtab');", true);
    }

按鈕代碼是:

     <td>
          <asp:Button ID="Button2" runat="server" Text="Search" Width="83px" OnClick="Button2_Click" />
     </td>

這是從展示如何執行此操作的示例項目中直接獲取的內容。 問題是什么也沒有發生。 就好像 Visual Studio 跳過了代碼一樣。 我還檢查了所有相同的項目都在代碼頂部的“使用”部分中。 我需要一個腳本管理器還是什么? 有沒有人對我可能忽略的內容有任何建議?

似乎您試圖在button click打開new window ,但_newtab無效。

你應該試試這種方式:

Page.ClientScript.RegisterStartupScript(this.GetType(), "OpenWindow", "window.open('http://www.microsoft.com', '_blank');", true);

Update:

Your .CS File

protected void ButtonClick(object sender, EventArgs e)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "OpenWindow", "window.open('http://www.microsoft.com', '_blank');", true);

    }

Your Aspx File

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebFormTest.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>

         <asp:Button Text="Open New Window" OnClick="ButtonClick"  type="button" runat="server" />

        </div>


    </form>

</body>
</html>

希望它會幫助你想要實現的目標。

暫無
暫無

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

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