1

我有这个下拉列表:

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

和 SqlDataSourceselect * all from [tbl_Cat]

它用于通过类别过滤数据库。它工作得很好,但它只显示tbl_Cat. 我还想要select allDropDownList 中的一个项目。

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

4

3 回答 3

1

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

 <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,则可以加载网格中的所有记录。

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

于 2013-04-24T12:15:13.340 回答
0

可能你有这个疑问,

DropDownList1.Items.Add(new ListItem("Select All", "0"));
于 2013-04-24T12:16:05.543 回答
0

以下是从代码隐藏绑定 DropDownList 的方法。访问此链接了解详情

 <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();
于 2013-04-24T11:59:19.657 回答