首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

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

2013-07-20 
如何获取DropDownList中某个item对应的index?如何获取DropDownList中某个item对应的index呢?例如item有a

如何获取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);//你像要的

热点排行