RegisterStartupScript creates duplicate launch page

Posted on 2006-05-11
Last Modified: 2010-05-19
Hi, This might be simple to fix, but has me baffled.

I am using ASP.NET 2.0 and one of my aspx pages uses RegisterStartupScript to launch a popup window aspx page. The popup works fine but what happens is that the original launch pages get's redrawn again. For the user to go back to the page before the launch page now they need to cross both the duplicate launch page and the orignal launch page. I need to avoid the duplicate start page.

Here's a sample of my code:

During the code behind handling of the button clicked event, after some needed processing I call:

Page.RegisterStartupScript("Pop", GetOpenNewWindowString("PopupDisplay.aspx", "pdisplay"));

And here's the method which generates the java script string:

private string GetOpenNewWindowString(string url, string windowName)
    string features = "status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes";
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    sb.Append("<script type=\"text/javascript\">\r\n");
    sb.Append("'" + url + "', '" + windowName + "', '" + features + "');\r\n");
    sb.Append("// -->\r\n");

    return sb.ToString();

Am I overlooking something simple? The problem occurs both in IE and Fire Fox.



Question by:p_m_becker
    LVL 25

    Expert Comment

    RegisterStartupScript always has issues perhaps your script is being registered multiple times. Use just Response.Write to write the JavaScript whenever you want:

    Response.Write(GetOpenNewWindowString("PopupDisplay.aspx", "pdisplay"));

    LVL 27

    Expert Comment

    your page is redrwan because your are posting back to the server. you should be using a client onclick instead of the server onlick to avoid posting to the server.
    LVL 27

    Expert Comment

    this is what I meant

    protected void Page_Load(object sender, EventArgs e)
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("<script type=\"text/javascript\">\r\n");
            sb.Append("var features='status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes;'\r\n");
            sb.Append("function openPopup(url,windowsName,features){");
            // sb.Append("'" + url + "', '" + windowName + "', '" + features + "');\r\n");
            sb.Append("// -->\r\n}");
            this.ClientScript.RegisterClientScriptBlock(this.GetType() ,"MyScript", sb.ToString());
            this.btnScript.Attributes.Add("onClick", @"openPopup('PopupDisplay.aspx','winname')");
    just change the button's name in the last line with yours. This way you dont postback to server therefor your page will not be redrawn

    LVL 5

    Expert Comment

    If you can call the javascript function even before the code behind handling of the button operations, you can try this.

    In the Page_Load function add the following line

    btbName.Attributes.Add("onClick", "GetOpenNewWindowString('PopupDisplay.aspx', "pdisplay")");

    With this code, your javascript function will be called irat and the your code behing function.

    Hope this helps.

    LVL 26

    Accepted Solution

    try changing the code
    sb.Append("'" + url + "', '" + windowName + "', '" + features + "');\r\n");
    sb.Append("    var MyWindow ='" + url + "', '" + windowName + "', '" + features + "');\r\n");
    i.e. returns a object .. so u have to declare a varible to accept it .. if u do not do that .. it will set the current page as the returned object .. that is what propbably is happening in ur case ...

    also for checking whether a script is already registered .. use
    If Not Me.IsStartupScriptRegistered("Pop") then

    I would not recommend using response.write for rendering javascripts to browser ..



    Author Comment

    When handling the postback event I need to do various processing before launching the new window, so using Java Script on the client side with a button wouldn't work without some major restructuring in the web app, so I'm stuck working on the server side.

    What I found that worked for me was a combination of two things: 1) Rejo’s insight that I was missing a variable to accept the object returned by and 2) And, this may sound lame but it worked, when handling the postback I set a flag so that in my overrided Render() method of my page, when the flag is set I write out the java script there.

    Thanks everyone for all the quick response/feedback!



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now