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; }
效果如下:
很直白,希望能帮助到你。