【问题标题】:DropDownList items and gridViewDropDownList 项目和 gridView
【发布时间】:2014-07-02 18:44:46
【问题描述】:

向所有好人问好。 我正在学习 ASP 并且有一个我无法解决的问题。这是困扰我的问题:

我有一个 gridView 列出所有客户及其所有信息(城市、地址、电话等)。我的想法是添加一个包含所有城市的 dropDownList,而不是当您在 gridView 中选择某个城市时,只会显示来自该城市的客户。我连接了所有内容,但现在我只能看到与 dropDownList 中的第一项相同城市的客户(从 [someTable] 中选择 *,其中 City=@City)。最后,这是一个问题:有什么方法可以插入第一个将被选中的项目,并且该项目的值类似于“全选”。选择第一个项目时,它应该让我看看所有城市的客户。有没有简单的解决办法?

【问题讨论】:

    标签: c# asp.net gridview drop-down-menu


    【解决方案1】:

    第一次绑定 DropDownList 后,在索引 0 处插入一个新的 ListItem:

    ddl.Items.Insert(0, new ListItem("- Please Select -"));
    

    然后,每当您再次绑定 GridView 时,首先检查 DropDownList 的索引,确保他们选择的不是第一个。

    if(ddl.SelectedIndex > 0)
    {
        gridview.DataSource = filteredList;
        gridview.DataBind();
    }
    else
    {
        gridview.DataSource = unfilteredList;
        gridview.DataBind();
    }
    

    【讨论】:

    • 效果很好。非常感谢。 :)
    【解决方案2】:

    我建议将City DropDownList 作为表单过滤器并在gridview 之外并填充所有城市名称。

    ddlCity.Items.Insert(0, new ListItem("select anything below", "0", true)
    

    然后当你得到数据时,你可以写如下内容

    if (ddlCity.SelectedIndex != 0){
       var list = select * from [someTable] // select all customers for all cities
    }
    else
    {
     // TODO : the required concatenation to send the selected city to sql       
     var list = select * from [someTable] where City = ddlCity.SelectedText // select only customers for selected city 
    }
    gv.DataSource = list;
    gv.DataBind();
    

    【讨论】:

      【解决方案3】:

      按照其他答案中的建议添加第一个列表项或:

      <asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true">
          <asp:ListItem Value="0">- Select -</asp:ListItem>
      </asp:DropDownList>
      

      如果 DropDownList1.SelectedValue 为 0,则在代码中绑定您的下拉列表,但不要将 @City 参数发送到您的 SQL:

      If DropDownList1.SelectedIndex > 0 Then
          'Send your parameter
      End If
      

      如下改变你的 SQL:

      SELECT * FROM [SomeTable] WHERE @City IS NULL OR City=@City
      

      如果要检索所有行,只需确保发送到 SQL 查询的变量为 NULL。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-30
        • 2019-09-19
        • 1970-01-01
        • 2011-01-18
        • 1970-01-01
        • 1970-01-01
        • 2011-01-16
        相关资源
        最近更新 更多