How to select all entries in DropDownList

I have this DropDownList:

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

and SqlDataSource select * all from [tbl_Cat]

Used to filter the database through a category. It works great, but it only shows the three categories that are in tbl_Cat. I also want the item select allin DropDownList.

DropDownList and datagrid are not created with code; can I enter the option "select all records" using the code?

+1
source share
3 answers

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

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

In the code behind the 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 the dropdown value is 0, if it is 0, then you can load all the records in the grid.

, -.

+1

DropDownList codebehind.

 <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

Perhaps you have this request,

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

All Articles