读书人

怎么获取DropDownList中某个item对应的

发布时间: 2013-07-20 11:07:48 作者: rapoo

如何获取DropDownList中某个item对应的index?
如何获取DropDownList中某个item对应的index呢?
例如item有'a','b','c',
获取'b'对应的index为1,好像没找到对应的方法?
[解决办法]
绑定数据源的时候
this.DropDownList1.DataTextField = "item";//显示的值
this.DropDownList1.DataValueField = "index";//获取dropdownlist中的值
DropDownList1.DataSource = 你的数据源;
this.DropDownList1.DataBind();
this.DropDownList1.SelectedValue就是了
[解决办法]
那就是SelectedIndex
[解决办法]
试试

int index = this.DropDownList1.Items.IndexOf(this.DropDownList1.Items.FindByValue("b"));//1

[解决办法]
引用:
试试
int index = this.DropDownList1.Items.IndexOf(this.DropDownList1.Items.FindByValue("b"));//1

正解

DropDownList2.Items.FindByText("b").Selected=true;
int index = DropDownList2.SelectedIndex;
或将value值直接为索引
DropDownList2.Items.FindByText("b").Value
[解决办法]
ListItem listItem = this.DropDownList1.Items.FindByText("1");//1为text
int index = this.DropDownList1.Items.IndexOf(listItem);//你像要的

读书人网 >asp.net

热点推荐