Ajax Update Panel ScriptManager.RegisterStartupScript redirect Parent from iframe not working

Cindy Mello 62 Reputation points
2021-06-18T18:10:51.147+00:00

I have an aspx page, c#, that loads in Ajax Update Panel. After page loads I override Page_Render so that I can save the page content as a pdf. After writing the file I want to redirect parent (my page is inside iframe) to another location on same site. I put the script to redirect inside void
private void RedirectWindowHome()
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectit", "parent.window.location = '/sites/service/zones/default.aspx'", true);
}
I call that from inside the overridden page render
protected override void Render(HtmlTextWriter writer)
{
//setup tw and write file..............

        if (!System.IO.File.Exists(pathString2))
        {
            File.WriteAllBytes(pathString2, pdfBytes);
        }
        else
        {
            Console.WriteLine("File \"{0}\" already exists.", fileName);
            return;
        }
        //file does write and I move to next line 
        RedirectWindowHome();
        // I even tried putting in button click and button clicks but no redirect
        Button1_Click(Button1, EventArgs.Empty);
 }

protected void Button1_Click(object sender, EventArgs e)
{
RedirectWindowHome();
}

so I am thinking that script does not register however on another form in the web app the same code
private void RedirectWindowHome()
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectit", "parent.window.location = '/sites/service/zones/default.aspx'", true);
}
is used on a button click, the submit button and the code fires and redirects.
Is it because I am not posting back? However that is why I tried using button click (programmatically) and that does not work.

Too many days with an old app - help?

Cindy

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,396 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,569 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yijing Sun-MSFT 7,071 Reputation points
    2021-06-21T02:45:22.5+00:00

    Hi @Cindy Mello ,
    As far as I think, redirectit will not process JavaScript that is written out to the output stream other than JavaScript that is registered through the ClientManager on postback. This is more related with the way javascript works. the addition of script nodes can't simply be done by adding text to the inner HTML property of a div (this is what is done when the script nodes sent on the content panel element that is returned from the server). when you use register, the server side sends those elements on a different xml element and these kind of elements gets "special" treatment on the client side.
    You could refer to below article:
    https://www.aspsnippets.com/Articles/Display-Show-JavaScript-Alert-Message-Box-from-Resource-file-in-ASPNet.aspx
    Best regards,
    Yijing Sun


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments