2012-10-21 97 views
0

我有一個問題,我下面的代碼,我需要做一個頁面顯示的圖表,以及一個下拉列表,以正確地更改圖表類型下拉列表不改變圖圖表

圖表顯示,但在下拉列表控件一樣沒有,任何人都可以幫忙?我也想選擇去3D在3種圖表類型

<%@ 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> 

回答

1

我沒有看到OnSelectedIndexChanged爲您列出DROPDOWNLIST任何功能。你告訴它回發,但不告訴它應該如何。

你應該是這樣的:

<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 
} 
+0

你建議不起作用的變化(onselectedindexchanged = 「ChartTypeList_SelectedIndexChanged」)和(保護無效ChartTypeList_SelectedIndexChanged(對象發件人,EventArgs e)條) – user1725978

+0

在什麼意義上的 「不工作」?你有錯誤嗎?發佈代碼作爲您的問題編輯創建的,所以我們對您嘗試做的事情有一定的瞭解。 – peroija

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>