如何在按钮单击事件中打开新的浏览器窗口? [英] How to open new browser window on button click event?

查看:81
本文介绍了如何在按钮单击事件中打开新的浏览器窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C#ASP.NET中的按钮单击事件中打开新的浏览器窗口?

How to open new browser window on button click event in C# ASP.NET?

请分享任何例子.

我正在执行以下代码.请让我知道我要去哪里了.

I am doing following code. Please let me know where I am going wrong.

btn_Click()
{
    if(condition==true)
    {
        this.Page.ClientScript.RegisterStartupScript(
            this.GetType(),
            "page_index_script2", 
            "openNewWindow();", 
            true
        );
    }
}

JavaScript函数是

And the JavaScript function is

function openNewWindow()
{
    alert('HI');
    window.open('http://www.stackoverflow.com');  
}

当我从javascript函数中运行代码时,Alert可以正常工作,但是新窗口无法打开.

When I run the code from javascript function Alert works but new window is not getting opened.

推荐答案

您可以使用类似这样的代码,可以根据需要调整高度和宽度

You can use some code like this, you can adjust a height and width as per your need

    protected void button_Click(object sender, EventArgs e)
    {
        // open a pop up window at the center of the page.
        ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", "var Mleft = (screen.width/2)-(760/2);var Mtop = (screen.height/2)-(700/2);window.open( 'your_page.aspx', null, 'height=700,width=760,status=yes,toolbar=no,scrollbars=yes,menubar=no,location=no,top=\'+Mtop+\', left=\'+Mleft+\'' );", true);
    }

这篇关于如何在按钮单击事件中打开新的浏览器窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆