弹出窗口没有打开按钮点击在asp.net

时间:2012-10-11 09:02:59

标签: c# asp.net button popup

我有示例asp.net按钮,我正在编写点击事件并尝试打开一个新的弹出窗口,由于某种原因弹出窗口没有打开,请知道这个问题是否与浏览器或代码有关

Test.aspx文件

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script language="javascript" type="text/javascript">

        function openNewWin(url) {

            var x = window.open(url, 'mynewwin', 'width=600,height=600,toolbar=1');

            x.focus();

        }

</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:Button ID="btnOpenPop" runat="server" Text="Open Pop"           

        onclick="btnOpenPop_Click" />
    </div>
    </form>
</body>
</html>

test.cs中

public partial class Test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {


        }

        protected void btnOpenPop_Click(object sender, EventArgs e)
        {
            string url = "http://www.dotnetcurry.com";

             ClientScript.RegisterStartupScript(this.GetType(), "OpenWin", "<script>openNewWin('" + url + "')</script>");




        }
    }

5 个答案:

答案 0 :(得分:3)

RegisterStartupScript用于注册应该在页面加载(客户端)上运行的脚本。请改用RegisterClientScript。

但是为什么要进行回发来运行javascript函数呢?而不是尝试执行代码服务器端,而不是客户端:

<asp:Button ID="btnOpenPop" runat="server" Text="Open Pop" OnClientClick="openNewWin(<%= URL %>)" />

并将URL声明为您班级中的媒体资源:

public partial class Test : System.Web.UI.Page
{
    public string URL {get;set;}
    //...
}

答案 1 :(得分:1)

您是否尝试在openNewWin来电后添​​加分号?喜欢这个

"<script>openNewWin('" + url + "');</script>"

答案 2 :(得分:1)

将此Javascript置于头部

 <script type="text/javascript">
            function popup(url) {
                var width = 600;
                var height = 600;
                var left = (screen.width - width) / 2;
                var top = (screen.height - height) / 2;
                var params = 'width=' + width + ', height=' + height;
                params += ', top=' + top + ', left=' + left;
                params += ', toolbar=no';
                params += ', menubar=no';
                params += ', resizable=yes';
                params += ', directories=no';
                params += ', scrollbars=no';
                params += ', status=no';
                params += ', location=no';
                newwin = window.open(url, 'd', params);
                if (window.focus) {
                    newwin.focus()
                }
                return false;
            }     
        </script>

现在点击按钮

调用此功能
 <asp:Button ID="Button2" runat="server" 
             OnClientClick="popup('addNewClients.aspx');" 
             Text="Add New Clients" />

答案 3 :(得分:0)

使用ScriptManger

ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "<script>$(function () {$('#popupID').modal({show:true,keyboard: false, backdrop: 'static'});});</script>", false);

答案 4 :(得分:-2)

使用jquery作为弹出窗口,

$('#btnOpenPop).bind('click',function() { 

var x = window.open(url, 'mynewwin', 'width=600,height=600,toolbar=1'); 

 x.focus(); 

});