2017-09-13 31 views
0

我正在创建一个将显示来自SQL数据库的特定数据的ASP.NET Web应用程序。我创建了3个DataSources每个特定的数据我想查看从DropDownList中选择。绑定到GridView显示的C#ASP.NET应用程序组合框问题

我的DropDownList编码C#侧是如下:在ASP.NET方

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      string SourceName = DropDownList1.SelectedValue; 
      GridView1.DataSourceID = SourceName; 
      GridView1.DataBind(); 
     } 

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一边是如下:

<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。它工作正常。但是,当Selected视图被选中到下拉列表中时,gridview保持不变。我一直在寻找一种显示新数据的方式。我错过了什么!我希望它只是一个简单的代码行,它将在运行时改变gridview。

回答

0

我忽略了一个巨大的游戏转换器。我没有改变正确的地方。由于所有的信息来自同一个数据库。我不得不写3个if语句来改变command.CommandText语句。在前。

If (DataName == "DataSource_General) 
{ 
    command.CommandText = " ... Select..."; 
} 

If (DataName == "DataSource_Portfolio") 
{ 
    command.CommandText = " ... Select ..."; 
} 

通过这样做我的数据显示正确。你也可以做这个配置DropDownList的项目值的命令文本,然后通过调用它:

String selectStatement = DropDownList.SelectedValue.ToString(); 
command.CommandText = selectStatement; 

通过该做的第二种方式,你实现更干净的代码。