【问题标题】:How to select all records in a DropDownList如何选择 DropDownList 中的所有记录
【发布时间】:2013-04-17 22:42:13
【问题描述】:

我有这个 DropDownList:

<asp:DropDownList ID="DropDownList1" 
    runat="server" 
    AutoPostBack="true" 
    DataSourceID="SqlDataSource1" 
    DataTextField="Categorie" 
    DataValueField="Cat_ID" 
>
</asp:DropDownList>

还有 SqlDataSource select * all from [tbl_Cat]

它用于通过类别过滤数据库。它运行良好,但仅显示tbl_Cat 中的三个类别。我还想要 DropDownList 中的 select all 项目。

DropDownList 和数据网格不是用代码隐藏的;是否可以通过代码隐藏输入“选择所有记录”选项?

【问题讨论】:

    标签: asp.net select drop-down-menu


    【解决方案1】:

    以下是从代码隐藏中绑定 DropDownList 的方法。详情请访问this link

     <asp:DropDownList ID="DropDownList1" runat="server">
     </asp:DropDownList>
    

    代码隐藏

    SqlConnection con = new SqlConnection(str);
    string com = "select * all from tbl_Cat";
    SqlDataAdapter adpt = new SqlDataAdapter(com, con);
    DataTable dt = new DataTable();
    adpt.Fill(dt);
    
    DropDownList1.DataSource = dt;
    DropDownList1.DataBind();
    DropDownList1.DataTextField = "Categorie";
    DropDownList1.DataValueField = "Cat_ID";
    DropDownList1.DataBind();
    

    【讨论】:

      【解决方案2】:

      您需要编写以下代码来帮助您选择类别的所有选项。

       <asp:DropDownList ID="DropDownList1" AutoPostBack="true" runat="server">
       </asp:DropDownList>
      

      在代码隐藏文件中

      SqlConnection con = new SqlConnection(str);
      string com = "select * all from tbl_Cat";
      SqlDataAdapter adpt = new SqlDataAdapter(com, con);
      DataTable dt = new DataTable();
      adpt.Fill(dt);
      
      DropDownList1.DataSource = dt;
      DropDownList1.DataBind();
      DropDownList1.DataTextField = "Categorie";
      DropDownList1.DataValueField = "Cat_ID";
      DropDownList1.DataBind();
      DropDownList1.Items.Insert(0, new ListItem("Select ALL", "0"));
      

      现在您可以检查下拉选择的值是否为 0,如果为 0,则可以加载网格中的所有记录。

      如果有什么我想念的,请告诉我。

      【讨论】:

      • 它说:编译器错误消息:BC30188:需要声明。在:SqlConnection con = new SqlConnection(str);
      【解决方案3】:

      可能你有这个疑问,

      DropDownList1.Items.Add(new ListItem("Select All", "0"));
      

      【讨论】:

        猜你喜欢
        • 2023-02-22
        • 1970-01-01
        • 1970-01-01
        • 2015-12-02
        • 2021-10-10
        • 1970-01-01
        • 2021-01-01
        • 2013-10-07
        • 2013-11-24
        相关资源
        最近更新 更多