RegisterStartupScript creates duplicate launch page

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.



Who is Participating?
RejojohnyConnect With a Mentor Commented:
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 ..


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

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.
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

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

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.

p_m_beckerAuthor Commented:
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!


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.