简体   繁体   中英

Response.Redirect and MSCharts

I've been working on a small application recently which consists of an index page containing a few reports run off a database, and then some forms to update the content of that page, all within ASP.NET using C#, in order to learn how to use MSCharts in future.

The index page can display any of 5 reports I have set up, chosen via a drop down box. By default, the first is selected such that there should always be a chart showing on the page.

I got the charts functioning and showing what I wanted them to on the index page fine , and the forms to update the database did their job too. However, when I clicked the "exit" button on one of these pages (which sent the user back to the index page), the chart that should have been displayed there was simply a red "x", as if the image did not exist. If I refreshed the page it appeared as it should do.

The Exit buttons at the time used Response.Redirect() to send the user back to the index page, and I spent some time confirming this was the correct method. In the end, I tried switching to use of Server.Transfer() instead, despite every site seeming to indicate that there was no major difference between the two in terms of the page execution cycle - only that Response.Redirect() sent a whole new request whereas Server.Transfer didn't.

Despite all that, though, using Server.Transfer() fixed the issue. Well, that's great!

Can anyone explain why this worked?

Update - Some code, in case it helps at all.

One of the events in question from the update forms - originally used Response.Redirect() of course.

    protected void Exit_Click(object sender, EventArgs e)
    {
        Server.Transfer("Default.aspx");
    }

The Page_Load from Default.aspx:

    protected void Page_Load(object sender, EventArgs e)
    {
        Report report = new Report(reportList.SelectedIndex);
        ChartPanel.Controls.Add(report.Chart);
    }

Outline of the Report class - this is basically a wrapper for the various reports I have specified in the createChartFromParameters() method. It simply does all the work of setting up the chart so it doesn't end up in the UI, and then lets the UI take the chart object itself. I can post the implementation if someone would find it useful, but it's ugly as anything and really quite long-winded, so I'd rather not.

public class Report
{
    private Chart chart { public get; }

    public Report(string title, string command, string x, string y, string label, string legend, SeriesChartType type)

    public Report(int presetChartNum)

    private void createChartFromParameters(string title, string command, string x, string y, string label, string legend, SeriesChartType type, int customcode = 0)  
}

I believe this is a misunderstanding of how the server object and response object perform the redirection

It has a lot to do with how the client is redirected to the new resource and from what end it does so, and what objects it has access to afterwards.

I could type it all out but there is a good resource here.

http://techahead.wordpress.com/2007/10/14/aspnet-servertransfer-vs-responseredirect/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM