简体   繁体   中英

Window.Open() is changing the page Layout on Internet Explorer

I have a page in asp.net called Home.aspx . It uses a masterpage and also have a button that is calling:

Response.Write("<script> window.open('page2.aspx','_blank'); </script>")

Result: The new tab is open with page2.aspx and if you return to Home.aspx , the page layout is modified, the central <div> is moved to the left of page.

What can i do to solve this problem? Remember that this is occurring only on Internet Explorer, Firefox works normally!

Thanks a lot.

Not going into the merit on why are you doing that, your Response.Write is likely killing HTML/CSS/JS output in the Home.aspx page and preventing it from loading properly. Try using:

ScriptManager.RegisterStartupScript(typeof(Page),
            "OpenPage2", "window.open('page2.aspx','_blank');", true);

Instead of the Response.Write, so as soon as the page loads it will open your window.

Generally if you want to write scripts from the code behind ScriptManager is your friend. Startup scripts happen after prerender, unload scripts happen on close and you can set them based on events like resize.

That your div moves is related to the page turn and not the specific button's server code. Using

    <script language="javascript" type="text/javascript">
        function mybutton() {
            window.open('page2.aspx', '_blank');
            return false;
        }
    </script>

with an onclientclick="mybutton" in your button asp tag avoids the postback in this instance. Beyond that you may have improper css or code that fires on page turn which will still move your div as soon as you postback.

In case somebody will find it useful, here is the exact answer which worked for me:

http://www.schnieds.com/2008/06/css-lost-on-postback-in-aspnet.html

So, in the OP case, instead of

Response.Write("<script> window.open('page2.aspx','_blank'); </script>")

that would work:

        String myScript = "<script> window.open('page2.aspx','_blank'); </script>";
        Page.ClientScript.RegisterStartupScript(this.GetType(), "myScript", myScript);

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