![](/img/trans.png)
[英]Is it possible to open popup window in button click event without using jquery in asp.net C#?
[英]popup window not opening on button click in asp.net
我有示例asp.net按鈕,我正在編寫click事件並嘗試打開一個新的彈出窗口,由於某種原因彈出窗口沒有打開,請知道這個問題是否與瀏覽器或代碼有關
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>");
}
}
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;}
//...
}
你有沒有嘗試在openNewWin
調用后添加分號? 像這樣
"<script>openNewWin('" + url + "');</script>"
把這個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>
現在在buttone點擊上調用它
<asp:Button ID="Button2" runat="server"
OnClientClick="popup('addNewClients.aspx');"
Text="Add New Clients" />
使用ScriptManger
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "<script>$(function () {$('#popupID').modal({show:true,keyboard: false, backdrop: 'static'});});</script>", false);
使用jquery彈出窗口,
$('#btnOpenPop).bind('click',function() {
var x = window.open(url, 'mynewwin', 'width=600,height=600,toolbar=1');
x.focus();
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.