views:

75

answers:

1

I have an ASP.NET2.0 web page with a Submit button. When the user clicks, I generate an XML file on the fly and return that as a result.

Here is the code:

protected void submitBtn_Click(object sender, EventArgs e)
    {
        string result = this.ProduceMyXmlResult();

        this.Response.Clear();
        this.Response.StatusCode = 200;
        this.Response.ContentType = "application/xml";
        this.Response.ContentEncoding = System.Text.Encoding.UTF8;
        this.Response.Write(result);
        this.Response.End();
    }

The piece of code does exactly what I want. However, the browser does not recognize the XML file as a new page, so the BACK button does not take me back to my original page. Why and how can I overcome that?

+4  A: 

The simplest way to do so, I think, would be to create a separate page that executes this code on Page_Load(), and redirect to it when the button is pressed.

The reason you have no backward navigation is because the browser is unaware the page has changed. Since the Submit button is preforming a postback, and you are returning XML data as the response to that postback, it appears to the browser as though this is just some transformation of the current page (just as though you'd, say, changed the text of a Label control).

The "correct" way to accomplish this would be with some type of HTTP handler, but I haven't the experience to suggest the proper way to do so and you already have working C# code-behind for this method.

JoshJordan
+1 better than my idea.
MusiGenesis
+1 go for a HTTP handler (i.e. implement IHttpHandler+web.config magic or add a ASHX file - a "page" like wrapper around IHttpHandler without web.config stuff)
veggerby