?
Solved

RegisterStartupScript creates duplicate launch page

Posted on 2006-05-11
6
Medium Priority
?
340 Views
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("<!--\r\n");
    sb.Append("    window.open('" + url + "', '" + windowName + "', '" + features + "');\r\n");
    sb.Append("// -->\r\n");
    sb.Append("</script>\r\n");

    return sb.ToString();
  }

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

Thanks!



Philip


0
Comment
Question by:p_m_becker
6 Comments
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 16661352
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"));

-Nauman.
0
 
LVL 27

Expert Comment

by:Sammy Ageil
ID: 16661550
Philip,
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.
0
 
LVL 27

Expert Comment

by:Sammy Ageil
ID: 16661902
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("<!--\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("\r\n");
        // sb.Append("    window.open('" + url + "', '" + windowName + "', '" + features + "');\r\n");
        sb.Append("       window.open(url,windowsName,features)\r\n");
        sb.Append("// -->\r\n}");
        sb.Append("</script>\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

HTH
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:arunrs
ID: 16662067
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.

arunrs
0
 
LVL 26

Accepted Solution

by:
Rejojohny earned 2000 total points
ID: 16665317
try changing the code
sb.Append("    window.open('" + url + "', '" + windowName + "', '" + features + "');\r\n");
to
sb.Append("    var MyWindow = window.open('" + url + "', '" + windowName + "', '" + features + "');\r\n");
i.e. window.open 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 ..

Rejo

0
 

Author Comment

by:p_m_becker
ID: 16670688
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 window.open() 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!


Philip

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

830 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