0

我下面的代码有问题,我需要创建一个显示图表的页面,以及一个下拉列表来更改图表类型

图表显示正确,但下拉列表控件不执行任何操作,有人可以帮忙吗?我还希望在 3 种图表类型上选择 3D

  <%@ Page Title="" Language="VB" MasterPageFile="~/Site.master" AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="admin_test" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">

  <script type="text/javascript" language="javascript">
  function showContent(i) {
      if (document.getElementById('statContent' + i).style.display == 'none') {
          document.getElementById('statContent' + i).style.display = 'block';
      } else {
          document.getElementById('statContent' + i).style.display = 'none';
      }
  } 
</script> 
            choose chart display type: 
<asp:dropdownlist id="ChartTypeList" runat="server" AutoPostBack="True" 
CssClass="spaceright" Width="112px">
                    <asp:ListItem Value="Column" Selected="True">Column 2D</asp:ListItem>
                    <asp:ListItem Value="Column">Column 3D</asp:ListItem>
                    <asp:ListItem Value="Line">Line 2D</asp:ListItem>
                    <asp:ListItem Value="Line">Line 3D</asp:ListItem>
                    <asp:ListItem Value="Pie">Pie 2D</asp:ListItem>
                    <asp:ListItem Value="Pie">Pie 3D</asp:ListItem>
            </asp:dropdownlist><br />

 <asp:Chart ID="Chart1" runat="server" DataSourceID="SqlDataSource1" >
  <series>
    <asp:Series Name="Series1" ChartType="Pie" XValueMember="suburb" 
      YValueMembers="username">
    </asp:Series>
  </series>
  <chartareas>
    <asp:ChartArea Name="ChartArea1">
    </asp:ChartArea>
  </chartareas>
</asp:Chart>


    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand="SELECT suburb, COUNT(username) AS username FROM member GROUP BY suburb">
        </asp:SqlDataSource>
</asp:Content>
4

2 回答 2

1

我没有看到OnSelectedIndexChanged为您列出的任何功能下拉列表。你告诉它回发,但没有告诉它应该做什么。

你应该有类似的东西:

<asp:dropdownlist id="ChartTypeList" runat="server" AutoPostBack="True" 
CssClass="spaceright" Width="112px" onselectedindexchanged="ChartTypeList_SelectedIndexChanged"> 

对于代码隐藏

protected void ChartTypeList_SelectedIndexChanged(object sender, EventArgs e)
{
//change the chart code
}
于 2012-10-21T03:20:30.700 回答
0

我自己解决了它,只是想我会为了任何在这里搜索的人的利益而发布它背后的正确代码:

Protected Sub DropDownList_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles DropDownList.SelectedIndexChanged
    'keep charts hidden when not selected
    If (DropDownList.SelectedValue = "hide") Then
        Chart1.Visible = False
        'Columns
    ElseIf (DropDownList.SelectedValue = "Column 3D") Then
        Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Column
        Chart1.ChartAreas(0).Area3DStyle.Enable3D = True
        Chart1.Visible = True
    ElseIf (DropDownList.SelectedValue = "Column") Then
        Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Column
        Chart1.ChartAreas(0).Area3DStyle.Enable3D = False
        Chart1.Visible = True

        'Pies
    ElseIf (DropDownList.SelectedValue = "Pie 3D") Then
        Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Pie
        Chart1.ChartAreas(0).Area3DStyle.Enable3D = True
        Chart1.Visible = True
    ElseIf (DropDownList.SelectedValue = "Pie") Then
        Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Pie
        Chart1.ChartAreas(0).Area3DStyle.Enable3D = False
        Chart1.Visible = True

        'Lines
    ElseIf (DropDownList.SelectedValue = "Line 3D") Then
        Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Line
        Chart1.ChartAreas(0).Area3DStyle.Enable3D = True
        Chart1.Visible = True
    ElseIf (DropDownList.SelectedValue = "Line") Then
        Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Line
        Chart1.ChartAreas(0).Area3DStyle.Enable3D = False
        Chart1.Visible = True
    End If
End Sub

这是正确的代码:

 <asp:dropdownlist id="DropDownList" runat="server" AutoPostBack="True" 
                    OnSelectedIndexChanged="DropDownList_SelectedIndexChanged" >
                    <asp:ListItem value="hide" Selected="True">Select</asp:ListItem> 
                    <asp:ListItem Value="Pie">Pie Chart</asp:ListItem>
                    <asp:ListItem Value="Pie 3D">Pie Chart 3D</asp:ListItem>
                    <asp:ListItem Value="Column" >Column Chart</asp:ListItem>
                    <asp:ListItem Value="Column 3D">Column Chart 3D</asp:ListItem>
                    <asp:ListItem Value="Line">Line Chart</asp:ListItem>
                    <asp:ListItem Value="Line 3D">Line Chart 3D</asp:ListItem>
                    </asp:dropdownlist><br />

 <asp:Chart ID="Chart1" runat="server" Width="450px" Height="450px" DataSourceID="SqlDataSource1" Visible="false"  >
      <series>
         <asp:Series Name="Series1" XValueMember="suburb" YValueMembers="username">
         </asp:Series>
      </series>
        <chartareas>
            <asp:ChartArea Name="ChartArea1">
            </asp:ChartArea>
        </chartareas>
 </asp:Chart>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand="SELECT suburb, COUNT(username) AS username FROM member GROUP BY suburb">
    </asp:SqlDataSource>
于 2012-10-29T02:33:49.167 回答