繁体   English   中英

ASP C#回发杀了我

[英]ASP C# Postback is killing me

我有一个按钮。 我按下按钮。 新的窗口打开。 是的 但是,在后台,带有按钮的页面正在重新加载。 如何获得仅打开窗口而不重新加载主页的按钮?

    protected void btnLookup_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);
}

当您编写如下内容时:

<ASP:Button runat="server">

...您不是在编写HTML。 您正在编写定义服务器端控件的XML,该控件具有某些功能。

每当您需要“常规” HTML标记时,只需编写没有ASP前缀和runat=server标记即可:

<button>My Button</button>

如果你想有一个按钮, 提交,您需要设置按钮类型 (默认为“提交”在某些浏览器)。 所以现在我们有了这个:

<button type="button">My Button</button>

如果要附加脚本,可以将其添加到标记中:

<button type="button" onclick="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+\'' );">My Button</button>

将脚本分离出来可能更干净:

<button type="button" onclick="OpenSomeWindow()">My button</button>

然后使用RegisterClientScript定义OpenSomeWindow ,或将其放在页面中包含的外部.js文件中。

button type = button将使它不提交页面。 您也可以使用jquery单击以打开窗口。 在jquery中,您可以执行e.preventDefault()来防止发生默认操作(提交页面)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM