ASP.Net ListBox SelectedIndex仅显示第一个匹配索引 [英] ASP.Net ListBox SelectedIndex shows first match index only
问题描述
大家好,
我在列表框中添加了许多项目,包括重复项.当我调用SelectedIndex属性时,仅获得其第一个匹配索引,而不是我选择的项目的索引.
请在这方面帮助我..
尖齿
为了您的更好理解,
我在设计"视图本身中添加了以下项目.
印度
澳大利亚
印度
孟加拉国
印度
并已编码为...
受保护的void ListBox1_SelectedIndexChanged(对象发送者,EventArgs e)
{
int sindex = ListBox1.SelectedIndex;
Response.Write(sindex);
}
当我在列表中选择WHICHEVER印度时,",
我只有"0"作为SelectedIndex.
这是我的问题!
帮帮我吧!
Jag
Hi All,
I have added many items in a ListBox, including duplicates. When I call SelectedIndex property, I get its first match index only, not the index of item which i selected.
Kindly help me in this..
Jag
For your better understanding,
I have added the following items, in the DESIGN view itself.
India
Australia
India
Bangladesh
India
and have coded like...
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int sindex = ListBox1.SelectedIndex;
Response.Write(sindex);
}
"When I select WHICHEVER India in the list, ",
I get only "0" as SelectedIndex.
This is my PROBLEM!!!
help me!
Jag
推荐答案
在我看来,您正在绑定page_Load
中的列表框的数据 对于每个后退page_load将触发,并且每次将数据绑定到listBox
因此每次您都不会获得所选项目的索引
解决方案是在绑定代码中添加if条件
In my guess you are data binding the ListBox in the page_Load
for every post back page_load will fire and every time data will be binded to the listBox
so every time you will not get the index of the selected item
solution for this is add an if condition to the binding code
if (!Page.IsPostBack )
{
// your binding code here
}
现在代码可以正常工作了
now the code works fine
Response.Write(sindex.ToString());
我不确定,但是尝试这样做
Response.Write(sindex.ToString());
Im not sure but try doing this
这篇关于ASP.Net ListBox SelectedIndex仅显示第一个匹配索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!