【问题标题】:How to open popup Page in New tab如何在新标签中打开弹出页面
【发布时间】:2014-04-23 14:22:43
【问题描述】:

我正在使用下面的代码在弹出窗口中打开页面。

ScriptManager.RegisterStartupScript(Page, GetType(Page), "OpenWindow",     "window.open('URL');", True)

但我想在新标签页中打开页面。谁能帮帮我。

【问题讨论】:

  • 能否请您指定您使用的浏览器?

标签: asp.net vb.net


【解决方案1】:

您需要将 JavaScript 调用设为用户发起的事件。

请参阅this问答了解更多详情(具体请参阅答案中的第 3 点)。

工作example

<input type="button" value="Click Me" onclick="openWindow()" />

function openWindow() { window.open('http://www.google.com/', '_blank'); }

【讨论】:

  • @angsuf 你的回答很好。我有微笑问题-->你能说一下如何从代码文件中执行此操作,因为这会导致弹出窗口和浏览器阻止弹出窗口..我们必须手动允许弹出窗口,然后在它工作后..
  • @Shirish 你的意思是从后面的代码中打开一个新窗口,还是你的意思是从后面的代码中创建 JavaScript 函数并设置 onclick 属性?
  • @angsuf 是的,我想从后面的代码打开新窗口,我正在使用这个函数.. ScriptManager.RegisterStartupScript(this, this.GetType(), "onclick", "javascript:window. open('../default.aspx','_blank');", true);但每次它会导致弹出窗口和浏览器阻止程序阻止所有浏览器中的弹出窗口..
  • @Shirish 您的代码只是运行脚本以在页面加载时打开弹出窗口。用户需要使用调用 JavaScript 函数的控件来启动弹出窗口。我提供的答案也与您有关。第6点6. Some popup blockers will allow windows opened from user initiated events, but not those opened otherwise.
【解决方案2】:

尝试用你的脚本替换它

window.open('URL', '_blank')

我认为是浏览器设置决定是否打开新窗口或标签。也可以看看。

找到这个;如果没有任何效果,试试这个 link

【讨论】:

  • 我用的是Chrome浏览器,已经设置好了。
  • 用IE试试看是否一样。这肯定是一些浏览器设置
【解决方案3】:

您可以使用ClientScript.RegisterStartupScript

我在 Google Chrome 中尝试过它的工作,但在 Firefox 中没有 在aspx代码中

 <asp:Button Text="" OnClick="Button1_Click" ID="Button1" runat="server" />

在 C# 中

protected void Button1_Click(object sender, EventArgs e)
{
    string queryString = "test.aspx" ;
      string newWin = "window.open('" + queryString + "','_blank');";
    ClientScript.RegisterStartupScript(this.GetType(), "pop", newWin, true);
}

在 VB 中

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)

    Dim queryString As String = "test.aspx" 
    Dim newWin As String = "window.open('" + queryString + "','_blank');";
    ClientScript.RegisterStartupScript(Me.GetType(), "pop", newWin, True)

End Sub

【讨论】:

  • 此代码无效。我尝试了此代码,但页面未打开。我的代码正在运行,但它打开了弹出窗口,我想打开新标签。
【解决方案4】:

为你检查这个例子,

 ScriptManager.RegisterClientScriptBlock(btnsave, this.GetType(), "Open",
"window.open('GenerateDCNoPrint.aspx?indentno=" + ddlindentno.SelectedItem.Text + 
"&orderno=" + ddlindentno.SelectedValue + "&lorryno=" + txtlorryno.Text.Trim() + 
"&depaturetime=" + txtdeparture.Text.Trim() + "&date=" + txtdate.Text + "', 
'_blank','dependent,resizable=yes,scrollbars=yes,top=0,height=600');", true);

【讨论】:

    【解决方案5】:

    你可以试试这个..在所有浏览器中为我工作......

    ScriptManager.RegisterStartupScript(this, this.GetType(), "onclick", "javascript:window.open(
    
    'URL','_blank','height=600px,width=600px,scrollbars=1');", true);
    

    如果您满意,请标记为答案..

    【讨论】:

      【解决方案6】:

      试试这个

      Page.ClientScript.RegisterStartupScript(this.GetType(), "OpenWindow", "window.open('WebForm2.aspx', '_blank');", true);
      

       string url = "WebForm2.aspx";
       string s = "window.open('" + url + "', 'popup_window', 'width=300,height=100,left=100,top=100,resizable=yes');";
       ClientScript.RegisterStartupScript(this.GetType(), "script", s, true);
      

      【讨论】:

        【解决方案7】:

        我已经搜索并尝试了许多解决方案,但最后,这对我有用。

        ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "newWindow", "window.open('google.com','_blank','status=1,toolbar=0,menubar=0,location=1,scrollbars=1,resizable=1,width=700,height=400');", true);
        

        【讨论】:

          猜你喜欢
          • 2019-09-09
          • 2019-12-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-08-18
          • 1970-01-01
          • 2014-03-25
          • 2016-07-14
          相关资源
          最近更新 更多