-1

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);
}
RazorSharp
  • 179
  • 1
  • 3
  • 15
  • 4
    Open the window from JavaScript instead of from server-side code? Don't make it a server button and it won't have to post to the server. – David Feb 10 '17 at 21:02
  • 1
    return false in js? – rach Feb 10 '17 at 21:03
  • 2
    This is why the gods graciously gave us the much saner MVC. Areas of concern are much clearer there. – itsme86 Feb 10 '17 at 21:04
  • This is why we use javascript. You don't need to bloody post back in order to do this. And this is why asp.net forms are dead. Long live MVC! –  Feb 10 '17 at 21:16
  • You do the same in web form as in mvc: Button and onclick event to open the window....I don't think that is the reason web form is dead....don't you think? – Ghini Antonio Feb 10 '17 at 21:50
  • If you want to use code behind to open your window and you don't want t reload the entire page, then you should use (ispostback) in conjunction with Update Panels. – DaniDev Feb 10 '17 at 22:57

2 Answers2

3

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.

Community
  • 1
  • 1
John Wu
  • 50,556
  • 8
  • 44
  • 80
1

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.

Brad Firesheets
  • 762
  • 1
  • 6
  • 20