簡體   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