views:

44

answers:

2

I have a page redirect that runs after a string is written to an MSword doc using WebClient and StringBuilder.

        HttpContext.Current.Response.Write(strHTMLContent);
        this.Page.Response.Redirect("http://www.google.com");
        HttpContext.Current.Response.End();

However the string never gets written (or it doesn't get the chance to) since the redirect happens instantly.

How can i make my redirect run only until the string Write has occurred?

Thanks guys

This is the code used to generate the MSWord:

        HttpContext.Current.Response.Charset = "";
        HttpContext.Current.Response.ContentType = "application/msword";
        string strFileName = "GenerateDocument" + ".doc";
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + strFileName);
        StringBuilder strHTMLContent = new StringBuilder();
A: 

The redirect ends all processing and instructs the browser where to point to next. What you may want to do is write the data you wish your user to see, but include a meta refresh or similar mechanism as outlined in the Wikipedia article.

Wayne Hartman
i tried this: HttpContext.Current.Response.Write(strHTMLContent); HtmlMeta meta = new HtmlMeta(); meta.HttpEquiv = "refresh"; meta.Content = "0;url=http://www.google.com/" this.Header.Controls.Add(meta); but for some reason it doesnt add the meta information to my html, though i can see no errors in the code.
leonnz
@leonnz Is there no way that you can include it as part of your strHTMLContent?
Wayne Hartman
The data in strHTMLContent is written to an MSWord document, so the meta redirect is not much good in there, since i need my browser window to redirect. Programmatically adding the meta tag has not worked...but it should, havn't figured out why yet.
leonnz
A: 

Correct me if I'm wrong, but if he were to put this in a try/catch/finally the refresh wouldn't execute until after the response was written right?

 try 
    {
      HttpContext.Current.Response.Write(strHTMLContent);
      HttpContext.Current.Response.End();
    }
 catch(Exception ex)
    {
      //handle exception
     }
 finally
     {
       this.Page.Response.Redirect("http://www.google.com");
     }
jon3laze
i got the same result with this, the redirect runs instantly. The whole thing is encapsulated in an onclick event if that matters, im not sure.
leonnz