在回发的selectedIndex丢失 - ASP.NET [英] selectedIndex is lost during postbacks - ASP.NET

查看:181
本文介绍了在回发的selectedIndex丢失 - ASP.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表框控件:



<asp:ListBox runat="server" id="lbox" autoPostBack="true" />

在code后面类似:

The code behind resembles:



private void Page_Load(object sender, System.EventArgs e)
{
    lbox.SelectedIndexChanged+=new EventHandler(lbox_SelectedIndexChanged);
    if(!Page.IsPostBack)
    {
        LoadData(); 	
    }
}
private LoadData()
{
    lbox.DataSource = foo();
    lbox.DataBind();
}
protected void lboxScorecard_SelectedIndexChanged(object sender, EventArgs e)
{
    int index = (sender as ListBox).selectedIndex;
}

我的问题是,当我的网页接收一个回(当用户进行选择的列表框),选择总是跳跃在列表框中的第一个项目,这样,在我的回调函数的索引变量始终为0。

My problem is that when my page receives a post back (when a user makes a selection in the listbox), the selection always "jumps" to the first item in the listbox, so that the index variable in my callback function is always 0.

看起来这可能是一个视图状态的问题?如何解决它,以便选择指数仍然通过回传?

Seems like this may be a viewstate problem? How can I fix it so that the selection index remains through the postback?

有没有AJAX怎么回事,这是.NET 1.0。

There is no ajax going on, this is .NET 1.0.

感谢。

修改1 JohnIdol已经得到了我近了一步,如果我转我原来的DataTable的数据源到一个ArrayList,然后一切正常...什么原因呢?

EDIT 1 JohnIdol has gotten me a step closer, If I switch the datasource from my original DataTable to an ArrayList, then everything work properly...what would cause this?

编辑2 事实证明,我的DataTable中有多个值是一样的,这样的指标被视为相同,具有相同价值的所有项目......感谢那些谁帮助!

Edit 2 It turns out that my DataTable had multiple values that were the same, so that the indexes were treated as the same as all items with the same value...thanks to those who helped!

推荐答案

什么是把foo()函数调用的输出?

What's the output of the foo() function call?

手动填充列表框中可以设置索引,以任何你想要的(全0为例) - 于是同样的事情可以发生设定在特定情况下给定的数据源(一个指定索引我想)。如果所有的项目指标为0的结果是SelectedIndexChanged事件不会引发(索引不改变!),一切都搞砸了:上回选择后会回到第一个项目列表中的

Populating manually the list box you can set indexes to whatever you want (all 0 for example) - so the same thing can happen setting a given dataSource under certain circumstances (one that specifies indexes I suppose). If all the item indexes are 0 the result is that the SelectedIndexChanged event is not raised (index does not change!) and everything is messed up: on post-back selection will go back to the first item in the list.

这可以解释它 - 我想不出别的 - 它工作正常,我在.NET 2.0我使用一个ArrayList用字符串来填充列表框。

This would explain it - I cannot think of anything else - it is working fine for me on .NET 2.0 I am using an ArrayList with strings to populate the listBox.

我可以重现你的问题是将所有指标为0的唯一方式。

The only way I can reproduce your issue is setting all indexes to 0.

我会说增加一个手表到列表框,检查指标在运行时,以确保他们不是全零。

I'd say add a watch to the ListBox and check the indexes at runtime to make sure they're not all zeroes.

这篇关于在回发的selectedIndex丢失 - ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆