使用sqldatasource中的列标题填充下拉列表

时间:2014-09-29 13:19:50

标签: asp.net

我有一个文本框,可以输入任意SQL SELECT语句。我想生成一个显示结果和图表的表格,以及y轴和x轴的下拉列表,用户可以在其中选择要在x轴上显示哪个结果列以及在y轴上显示哪个结果列。轴。

目前,我使用文本框中的文本来更新SqlDataSource的SelectCommand。我有一个GridView绑定到数据源,AutoGenerateColumns设置为true。这很好地生成了我的桌子。

我将图表绑定到相同的数据源,我想将两个下拉列表填充到结果列标题中,然后更新XValueMember&图表的XValueMember属性。我遇到的问题是如何确定列名以填充下拉列表。由于GridView自动生成这些,我尝试从那里读取,但我得到的是旧值,而不是最近的SELECT语句中的值。

在我的default.aspx中,我有:

<asp:Chart ID="Chart1" runat="server" DataSourceID="SqlDataSource1">
    <Series>
        <asp:Series Name="Series1" ChartType="Line"></asp:Series>
    </Series>
    <ChartAreas>
        <asp:ChartArea Name="ChartArea1"></asp:ChartArea>
    </ChartAreas>
</asp:Chart>
<asp:DropDownList ID="ddlX" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlX_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="ddlY" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlY_SelectedIndexChanged">
</asp:DropDownList>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True" DataSourceID="SqlDataSource1" AllowSorting="True">
</asp:GridView>
<asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString='<%$ ConnectionStrings:myConnectionString %>'</asp:SqlDataSource>

和代码隐藏(C#)

protected void ddlX_SelectedIndexChanged(object sender, EventArgs e)
{
    Chart1.Series[0].XValueMember = this.ddlX.SelectedValue;
}
protected void ddlY_SelectedIndexChanged(object sender, EventArgs e)
{
    Chart1.Series[0].YValueMembers = this.ddlY.SelectedValue;
}

但是如何填充下拉框。

1 个答案:

答案 0 :(得分:0)

我能够获得一个有效的解决方案:在page_load期间读取gridview列并更新图表XSeries和YSeries。代码现在看起来像:

Default.aspx的

<asp:Chart ID="Chart1" runat="server" DataSourceID="SqlDataSource1">
    <Series>
        <asp:Series Name="Series1" ChartType="Line"></asp:Series>
    </Series>
    <ChartAreas>
        <asp:ChartArea Name="ChartArea1"></asp:ChartArea>
    </ChartAreas>
</asp:Chart>
<asp:DropDownList ID="ddlX" runat="server" AutoPostBack="True">
</asp:DropDownList>
<asp:DropDownList ID="ddlY" runat="server" AutoPostBack="True">
</asp:DropDownList>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True" DataSourceID="SqlDataSource1" AllowSorting="True">
</asp:GridView>
<asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString='<%$ ConnectionStrings:myConnectionString %>'</asp:SqlDataSource>

Default.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    // get column names from gridview and populate ddl
    int cols = GridView1.HeaderRow.Cells.Count;
    int selInd = ddlX.SelectedIndex;
    ddlX.Items.Clear();
    for (int i = 0; i < cols; i++)
    {
        LinkButton Link = GridView1.HeaderRow.Cells[i].Controls[0] as LinkButton;
        string col = Link.Text;
        ddlX.Items.Insert(i, col);
    }
    ddlX.SelectedIndex = selInd;
    cols = GridView1.HeaderRow.Cells.Count;
    selInd = ddlY.SelectedIndex;
    ddlY.Items.Clear();
    for (int i = 0; i < cols; i++)
    {
        LinkButton Link = GridView1.HeaderRow.Cells[i].Controls[0] as LinkButton;
        string col = Link.Text;
        ddlY.Items.Insert(i, col);
    }
    ddlY.SelectedIndex = selInd;
    // set chart axes
    Chart1.Series[0].XValueMember = ddlX.SelectedValue;
    Chart1.Series[0].YValueMembers = ddlY.SelectedValue;

}