How to select all records in a DropDownList

后端 未结 3 1681
遇见更好的自我
遇见更好的自我 2021-01-24 02:06

I have this DropDownList:



        
相关标签:
3条回答
  • 2021-01-24 02:42

    following is the way to bind DropDownList from codebehind. visit this link for details

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

    CODE BEHIND

    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();
    
    0 讨论(0)
  • 2021-01-24 02:47

    You need to write the below code which help you to select all option for category.

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

    In Code behind file

    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"));
    

    Now you can check if drop down selected value is 0, if it is 0 then you can load all the records in the grid.

    Let me know if any thing i miss.

    0 讨论(0)
  • 2021-01-24 02:49

    May be you have this query,

    DropDownList1.Items.Add(new ListItem("Select All", "0"));
    
    0 讨论(0)
提交回复
热议问题