简体   繁体   中英

ASP C# Postback is killing me

I have a button. I push the button. A new window opens. Yeah. But, in the background, the page with the button is reloading. How do I get the button to ONLY open the window and NOT reload the main page?

    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);
}

When you write something like this:

<ASP:Button runat="server">

...you are not writing HTML. You are writing XML that defines a server-side control, which has certain functionality.

Whenever you want "normal" HTML markup, just write it without the ASP prefix and without runat=server :

<button>My Button</button>

If you want a button to not submit, you need to set the button type (it defaults to "SUBMIT" on some browsers). So now we have this:

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

And if you want to attach script, you can just add it to the markup:

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

It might be cleaner to separate the script out:

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

And then use RegisterClientScript to define OpenSomeWindow , or put it in an external .js file that is included on the page.

button type=button will get it to not submit the page. Also you can use jquery to do the click to open the window. In jquery you can do e.preventDefault() to prevent the default action (submit page) from happening.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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