如何使用下拉列表选择数据源

本文关键字:数据源 选择 下拉列表 何使用 | 更新日期: 2023-09-27 18:30:41

>我有这样的下拉列表:

 <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
    Height="22px" Width="650px">
     <asp:ListItem> </asp:ListItem>
     <asp:ListItem>spy</asp:ListItem>
     <asp:ListItem>uk</asp:ListItem>
     <asp:ListItem>it</asp:ListItem>
</asp:DropDownList>

我使用csv文件作为数据。喜欢这个:

spy.csv
uk.csv
it.csv

我可以为 3 个 csv 文件添加 3 个 sqldata 源并链接网格视图等。

但是我需要做的是,当我在我的下拉列表中选择间谍时,网格视图使用间谍数据源,当我选择英国时,网格视图使用ukdatasource。

我该怎么办?


数据源

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
    SelectCommand="SELECT * FROM [spy.csv] WHERE ([Hostname] = ?)">
    <SelectParameters>
    <asp:ControlParameter ControlID="DropDownList1" Name="Hostname" 
        PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
    SelectCommand="SELECT * FROM [uk.csv] WHERE ([Hostname] = ?)">
    <SelectParameters>
        <asp:ControlParameter ControlID="DropDownList1" Name="Hostname" 
            PropertyName="SelectedValue" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
    SelectCommand="SELECT * FROM [it.csv] WHERE ([Hostname] = ?)">
    <SelectParameters>
        <asp:ControlParameter ControlID="DropDownList1" Name="Hostname" 
            PropertyName="SelectedValue" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>

如何使用下拉列表选择数据源

这将是太多的信息,无法发布到评论中,所以我会把它写在答案中。请让我知道它是否符合您正在寻找的内容。

前端:

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
    Height="22px" Width="650px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
     <asp:ListItem> </asp:ListItem>
     <asp:ListItem>spy</asp:ListItem>
     <asp:ListItem>uk</asp:ListItem>
     <asp:ListItem>it</asp:ListItem>
 </asp:DropDownList>
 <asp:GridView ID="GridViewID" .... >
 </asp:GridView>

代码隐藏

protected void DropDownList1_SelectedIndexChanged(Object sender, EventArgs e) {
  if(DropDownList1.SelectedIndex != 0)
  {
      var csvFile = DropDownList1.SelectedValue + ".csv";
      DataTable dt = //Convert csvFile to dataTable
      GridViewID.DataSource= dt;
      //This is an alternate method, setting the Select Command of a given SqlDataSource
      SqlDataSourceID.SelectCommand = String.Format("SELECT * FROM [{0}] WHERE ([Hostname] = ?)",csvFile);
  } 
}

这是我找到的有关如何将 CSV 转换为 GridView 数据源的简短指南;http://www.c-sharpcorner.com/UploadFile/hrojasara/how-you-can-use-csv-file-as-data-source-of-gridview/

编辑:添加了处理更改下拉列表的替代方法。

让我知道它是否有帮助!

我会同意桑德的评论。

或者,我将有三个不同的数据源(每个数据源都有不同的选择命令)。我会相应地连接三个。

根据我的下拉列表选择,我会使与下拉列表的选定选项对应的网格可见。

对于一个小问题来说,这是一个很长的机会。但只能通过设计师来实现。

像这样的东西?

受保护的无效DropDownList1_SelectedIndexChanged(对象发送方,事件参数 e) {

  if(DropDownList1.SelectedItem.Text == "1ofthevalues")
  {
  yourGridview.DataSource= yourDataSourceID
    yourGridview.DataBind();
  } 
}

哦,将 {OnSelectedIndexChanged= "DropDownList1_SelectedIndexChanged"} 添加到您的下拉列表中