首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >asp:DropDownList用法

asp:DropDownList用法

作者头像
红目香薰
发布于 2022-11-29 14:51:45
发布于 2022-11-29 14:51:45
4.6K0
举报
文章被收录于专栏:CSDNToQQCodeCSDNToQQCode

DropDownList前台:

代码语言:javascript
复制
  <asp:DropDownList runat="server" ID="drop" AutoPostBack="true" OnSelectedIndexChanged="drop_SelectedIndexChanged"></asp:DropDownList>

DropDownList后台:

代码语言:javascript
复制
        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();
            }
        }

获取值的后台方法:

代码语言:javascript
复制
        protected void drop_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectedValue = this.drop.SelectedValue;
            int selectedIndex = this.drop.SelectedIndex;
            ListItem selectedItem = this.drop.SelectedItem;
        }

效果如下:

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-08-28,如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档