asp:DropDownList用法

简介: asp:DropDownList用法

DropDownList前台:


  <asp:DropDownList runat="server" ID="drop" AutoPostBack="true" OnSelectedIndexChanged="drop_SelectedIndexChanged"></asp:DropDownList>


DropDownList后台:


        protected void Page_Load(object sender, EventArgs e)
        {
            //EF引入数据,或者自定义的list<对象>集合
            using (testEntities db =new testEntities()) {
                this.drop.DataSource = db.users.ToList();
                //显示的
                this.drop.DataTextField = "userName";
                //实际值
                this.drop.DataValueField = "id";
                this.drop.DataBind();
            }
        }


获取值的后台方法:


        protected void drop_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectedValue = this.drop.SelectedValue;
            int selectedIndex = this.drop.SelectedIndex;
            ListItem selectedItem = this.drop.SelectedItem;
        }


效果如下:


微信图片_20220115104213.png


很直白,希望能帮助到你。

相关文章
|
2月前
|
存储 开发框架 NoSQL
ASP.NET WEB——项目中Cookie与Session的用法
ASP.NET WEB——项目中Cookie与Session的用法
36 0
|
2月前
|
SQL 开发框架 前端开发
ASP.NET WEB项目中GridView与Repeater数据绑定控件的用法
ASP.NET WEB项目中GridView与Repeater数据绑定控件的用法
34 0
|
10月前
|
开发框架 JavaScript .NET
Asp.net 控件用法汇总-RadioButtonList、DropDownList、button、Checkbox...(续)
Asp.net 控件用法汇总-RadioButtonList、DropDownList、button、Checkbox...(续)
88 0
|
10月前
|
开发框架 JavaScript 前端开发
Asp.net 控件用法汇总-RadioButtonList、DropDownList、button、Checkbox...
Asp.net 控件用法汇总-RadioButtonList、DropDownList、button、Checkbox...
62 0