DropDownList控件的使用方法

1. 使用代码添加数据

复制代码
        <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>
       
//1.1使用代码添加数据
        ListItem item = new ListItem(".net","1");
        this.DropDownList1.Items.Add(item);
        item = new ListItem("java", "2");
        this.DropDownList1.Items.Add(item);
        item = new ListItem("php", "3");
        this.DropDownList1.Items.Add(item);
        item = new ListItem("选择全部", "0");
        this.DropDownList1.Items.Insert(0, item);//在第一个索引处添加‘选择全部’选项
复制代码

2. 使用代码绑定数据源

复制代码
        <asp:DropDownList ID="DropDownList2" runat="server">
        </asp:DropDownList>
 
//2.1使用代码绑定数据源
        CategoriesManager manager = new CategoriesManager();
        this.DropDownList2.DataSource=manager.GetDataTable();
        this.DropDownList2.DataTextField = "name";
        this.DropDownList2.DataValueField = "id";
        this.DropDownList2.DataBind();
        this.DropDownList2.Items.Insert(0,new ListItem("选择全部", "0"));
复制代码

3. 使用数据绑定控件绑定数据源

        <asp:DropDownList ID="DropDownList3" runat="server" 
            DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="Id">
        </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:BookShopConnectionString %>" 
            SelectCommand="SELECT [Id], [Name] FROM [Categories]"></asp:SqlDataSource>

 

posted on   逍遥云天  阅读(2326)  评论(0编辑  收藏  举报

编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示