ASP.net图表刷新页面

时间:2013-07-24 08:39:16

标签: asp.net

我意识到asp:图表控制器正在刷新页面。

例如,如果我点击按钮显示图表,则刷新当前页面,但我不希望它。

假设我想要显示两个图表。我的default.aspx文件中有以下代码(使用工具箱生成的代码):

    <div>
    <asp:Chart ID="Chart1" runat="server">
        <series>
            <asp:Series Name="Series1">
            </asp:Series>
        </series>
        <chartareas>
            <asp:ChartArea Name="ChartArea1">
            </asp:ChartArea>
        </chartareas>
    </asp:Chart>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    </div>
    <div>
    <asp:Chart ID="Chart2" runat="server">
        <series>
            <asp:Series Name="Series1">
            </asp:Series>
        </series>
        <chartareas>
            <asp:ChartArea Name="ChartArea1">
            </asp:ChartArea>
        </chartareas>
    </asp:Chart>
        <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button" />
    </div>

default.aspx.cs文件中的后置代码:

    protected void Button1_Click(object sender, EventArgs e)
    {
        double[] yValues = { 71.15, 23.19, 5.66 };
        string[] xValues = { "AAA", "BBB", "CCC" };
        Chart1.Series["Series1"].Points.DataBindXY(xValues, yValues);
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        double[] yValues = { 71.15, 23.19, 5.66 };
        string[] xValues = { "DDD", "EEE", "FFF" };
        Chart2.Series["Series1"].Points.DataBindXY(xValues, yValues);
    }

我的问题是,当我点击Button1时,会出现我的第一个图表。但是当我点击Button2时,第一个图表消失,第二个图表出现。

我想保持两个图表显示!

有可能吗?怎么样?

1 个答案:

答案 0 :(得分:0)

页面上有2个按钮,每个按钮触发自己的点击事件。这样,你当然只会看到一张图表。

如果你想一直看到这两个,那就这样做(两个按钮都指向一个共享的事件处理程序):

<div>
<asp:Chart ID="Chart1" runat="server">
    <series>
        <asp:Series Name="Series1">
        </asp:Series>
    </series>
    <chartareas>
        <asp:ChartArea Name="ChartArea1">
        </asp:ChartArea>
    </chartareas>
</asp:Chart>
    <asp:Button ID="Button1" runat="server" onclick="ChartButtons_Click" Text="Button" />
</div>
<div>
<asp:Chart ID="Chart2" runat="server">
    <series>
        <asp:Series Name="Series1">
        </asp:Series>
    </series>
    <chartareas>
        <asp:ChartArea Name="ChartArea1">
        </asp:ChartArea>
    </chartareas>
</asp:Chart>
    <asp:Button ID="Button2" runat="server" onclick="ChartButtons_Click" Text="Button" />
</div>

在您的CS文件中:

protected void ChartButtons_Click(object sender, EventArgs e)
{
    double[] yValues = { 71.15, 23.19, 5.66 };
    string[] xValues = { "AAA", "BBB", "CCC" };
    Chart1.Series["Series1"].Points.DataBindXY(xValues, yValues);

    yValues = { 71.15, 23.19, 5.66 };
    xValues = { "DDD", "EEE", "FFF" };
    Chart2.Series["Series1"].Points.DataBindXY(xValues, yValues);
}