【发布时间】:2018-02-22 14:31:02
【问题描述】:
我正在创建一个 ASP.NET Web 应用程序,它将显示来自 SQL 数据库的特定数据。我创建了 3 个数据源,每个特定于我想从 DropDownList 中选择的数据查看。
我的DropDownList Coding C#端如下:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string SourceName = DropDownList1.SelectedValue;
GridView1.DataSourceID = SourceName;
GridView1.DataBind();
}
ASP.NET 端的DropDownList 编码:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="DataSource_Gerneral">General Overview</asp:ListItem>
<asp:ListItem Value="DataSource_Portfolio">Portfolio</asp:ListItem>
<asp:ListItem Value="DataSource_ErrorLog">Error Log</asp:ListItem>
</asp:DropDownList>
ASP.NET端的Grid View编码如下:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BorderStyle="Inset" CellPadding="4" DataKeyNames="Call_Sign" DataSourceID= "DataSource_Gerneral" Font-Size="Smaller" ForeColor="#333333" GridLines="None" Height="16px" Width="155px" AllowPaging="True" AllowSorting="True" ClientIDMode="AutoID" HorizontalAlign="Justify" PageSize="100" OnSelectedIndexChanged="GridView1_SelectedIndexChanged1">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="Call_Sign" HeaderText="Call_Sign" ReadOnly="True" SortExpression="Call_Sign" />
<asp:BoundField DataField="Current_Price" HeaderText="Current_Price" SortExpression="Current_Price" />
<asp:BoundField DataField="Stock_Market" HeaderText="Stock_Market" SortExpression="Stock_Market" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
我已经测试了下拉列表以确保它实际上正在更改 DatasourceID。它工作正常。 However When Selected view is Selected into the dropdownlist, the gridview stays the same.我一直在寻找一种显示新数据的方法。我是不是错过了什么!我希望这只是一行简单的代码,会在运行时改变gridview。
【问题讨论】:
标签: c# asp.net gridview drop-down-menu