C# 如何使用dropdownlist选择数据源

C# 如何使用dropdownlist选择数据源,c#,asp.net,csv,gridview,C#,Asp.net,Csv,Gridview,我有这样的下拉列表: <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: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>
我可以为3个csv文件添加3个sqldatasources,并链接gridview等

但我需要这样做,当我在dropdownlist上选择spy时,gridview使用spydatasource,当我选择uk时,gridview使用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>

像这样的东西

受保护的void DropDownList1\u SelectedIndexChanged(对象发送方,事件参数e) {

哦,然后将{OnSelectedIndexChanged=“dropdownlist 1\u SelectedIndexChanged”}添加到您的dropdownlist

类似的内容

受保护的void DropDownList1\u SelectedIndexChanged(对象发送方,事件参数e) {


哦,请将{OnSelectedIndexChanged=“dropdownlist 1\u SelectedIndexChanged”}添加到您的dropdownlist中

这将是太多的信息,无法发布到评论中,因此我会将其写在回答中。请让我知道它是否符合您的要求

前端:

<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>
下面是一个关于如何将CSV转换为GridView数据源的简短指南;

编辑: 添加了处理更改DropDownList的替代方法


让我知道它是否有用!

这将是太多的信息,无法发布到评论中,因此我会将其写在回答中。请让我知道它是否符合您的要求

前端:

<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>
下面是一个关于如何将CSV转换为GridView数据源的简短指南;

编辑: 添加了处理更改DropDownList的替代方法


如果有帮助,请告诉我!

我同意桑德的意见

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

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


对于一个小问题来说,这是一个很难解决的问题。但只需通过设计师即可解决。

我同意桑德的意见

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

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



对于一个小问题来说,这是一个很难解决的问题。但是只需通过设计器就可以了。

请提供更多的代码。您当前如何使用数据源?没有代码,我只使用标准的sqldatasource。添加了我的第一篇文章。在您的
sqldatasource
回调函数中更改
SelectCommand
如何您将添加到您的
DropDownList
?我怎么做?我尝试([CSV]=?)并为此和link dropdownlist添加了控制参数,但不起作用。请提供更多代码。您当前如何使用数据源?没有代码,我只使用标准的sqldatasource。添加了我的第一篇文章。在您添加的回调函数中更改
sqldatasource
上的
SelectCommand
如何o您的
DropDownList
?如何执行此操作?我尝试([CSV]=?)并为此和链接DropDownList添加了控制参数,但不起作用。编译器错误消息:CS1520:方法必须具有返回类型DropDownList1\u SelectedIndexChanged(对象发送方,事件参数e){哦,等等,将第一行更改为:protectedvoid DropDownList1\u SelectedIndexChanged(Object sender,EventArgs e){编辑您的帖子后没有错误,但对gridview无效。不返回任何内容。另外:添加您的gridview.DataBind();在SelectedIndexChangedDedDataBind中,但返回白色网格视图。顺便说一句,数据源工作正常。编译器错误消息:CS1520:方法必须具有返回类型DropDownList1_SelectedIndexChanged(Object sender,EventArgs e){哦,等等,将第一行更改为:protected void DropDownList1_SelectedIndexChanged(Object sender,EventArgs e){编辑您的文章后没有错误,但对gridview无效。不返回任何内容。另外:添加您的gridview.DataBind();在SelectedIndexChangedDed数据绑定中,但返回白色gridview。顺便说一句,数据源工作正常。我甚至不知道可以将csv转换为datatable,很好。通过使用此方法,您不必无缘无故地在前端拥有大量数据源。我如何更改Sqldatasource上的select命令?我实际上需要这样:select*从[$x.csv]和我的dropdownlist上的$x平均值。我是这样使用的吗?如果你使用我的方法,你实际上不需要
SqlDataSource
,但如果你必须使用它,我想你可以设置它。查看我的后期编辑。我甚至不知道你可以将csv转换为datatable,很好的一个使用这种方法,你不必在前端有一堆数据源没有理由。我如何更改Sqldatasource上的select命令?我需要这样:从[$x.csv]中选择*并在我的dropdownlist上选择$x平均值。我是否这样使用?如果使用我的方法,您实际上不需要
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);
  } 
}