views:

16

answers:

1

An ASP.NET Chart control is refreshed on page post back -- but I don't want it to be.

In my aspx, I have:

    <asp:Panel runat="server" ID="PanelRight" CssClass="roadmapRight">
        <asp:Chart ID="ChartRemainingDaysHistory" runat="server">
            <Series>
            </Series>
            <ChartAreas>
                <asp:ChartArea Name="ChartAreaLineGraph">
                    <AxisY Title="Days Remaining" />
                    <AxisX Title="Date" IsLabelAutoFit="True">
                        <LabelStyle Angle="90" Interval="2" />
                    </AxisX>
                </asp:ChartArea>
            </ChartAreas>
        </asp:Chart>
    </asp:Panel>

In the code behind:

protected void Page_Load(object sender, EventArgs e)
{
    /* A bunch of code that needs to run */

    if(IsPostback) return;  // return if it is postback

    // On first run build the graph

    IQueryable<int> _users = 
        (metaPlanningDataContext.TasksCurrents.Where(....)).Distinct();

    var s = new Series { 
        ChartType = SeriesChartType.Area, 
        Legend = "Engineer", 
        LegendText = "someName" };
    foreach (DateTime dateTimeKey in someDataSet.Keys)
    {
        s.Points.AddXY(dateTimeKey.ToOADate(), someDataSet[dateTimeKey]);
    }
}

The problem is that on a postback from some other control (Telerik RadGrid) the chart series contents disappear.

How can I secure the control from being destroyed in the postback?

+1  A: 

Is the problem that after a postback the chart isn't what was generatd on the page load? I think there is a settingon the chart called EnableViewState...you could try setting that to true

<asp:Panel runat="server" ID="PanelRight" CssClass="roadmapRight">
    <asp:Chart ID="ChartRemainingDaysHistory" runat="server" EnableViewState="true">
        <Series>
        </Series>
        <ChartAreas>
            <asp:ChartArea Name="ChartAreaLineGraph">
                <AxisY Title="Days Remaining" />
                <AxisX Title="Date" IsLabelAutoFit="True">
                    <LabelStyle Angle="90" Interval="2" />
                </AxisX>
            </asp:ChartArea>
        </ChartAreas>
    </asp:Chart>
</asp:Panel>

Leave a comment if I'm on the wrong tram...

davidsleeps
That did it! Thanks!
Matt