C# selectedIndex 在回发期间丢失 - ASP.NET
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/338472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
selectedIndex is lost during postbacks - ASP.NET
提问by mmattax
I have a list box control:
我有一个列表框控件:
<asp:ListBox runat="server" id="lbox" autoPostBack="true" />
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;
}
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.
我的问题是,当我的页面收到回发时(当用户在列表框中进行选择时),选择总是“跳转”到列表框中的第一项,因此我的回调函数中的索引变量始终为 0。
Seems like this may be a viewstate problem? How can I fix it so that the selection index remains through the postback?
似乎这可能是一个视图状态问题?如何修复它以便选择索引通过回发保持?
There is no ajax going on, this is .NET 1.0.
没有 ajax,这是 .NET 1.0。
Thanks.
谢谢。
EDIT 1JohnIdol 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?
编辑 1JohnIdol 让我更近了一步,如果我将数据源从原始 DataTable 切换到 ArrayList,那么一切正常……什么会导致这种情况?
Edit 2It 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!
编辑 2事实证明,我的 DataTable 有多个相同的值,因此索引被视为与具有相同值的所有项目相同......感谢那些提供帮助的人!
采纳答案by JohnIdol
What's the output of the foo() function call?
foo() 函数调用的输出是什么?
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.
手动填充列表框,您可以将索引设置为您想要的任何内容(例如,全部为 0) - 因此在某些情况下(我想是指定索引的)设置给定数据源可能会发生同样的事情。如果所有项目索引都为 0,则结果是 SelectedIndexChanged 事件未引发(索引未更改!)并且一切都搞砸了:在回发时选择将返回到列表中的第一个项目。
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.
这可以解释它 - 我想不出其他任何东西 - 它在 .NET 2.0 上对我来说很好用我正在使用带有字符串的 ArrayList 来填充 listBox。
The only way I can reproduce your issue is setting all indexes to 0.
我可以重现您的问题的唯一方法是将所有索引设置为 0。
I'd say add a watch to the ListBox and check the indexes at runtime to make sure they're not all zeroes.
我会说向 ListBox 添加一个监视并在运行时检查索引以确保它们不全为零。
回答by Briggie Smalls
I dont know if it makes a difference or not, but i generally attach my controls to events on the front page rather than in the codebehind. In your example i would have done:
我不知道它是否有区别,但我通常将控件附加到首页上的事件而不是代码隐藏中。在你的例子中,我会这样做:
<asp:ListBox runat="server" id="lbox" autoPostBack="true" OnSelectedIndexChanged="lboxScorecard_SelectedIndexChanged" />
Other than that, i would verify that the ViewState is enabled. ViewState can be turned of at the control, page, & even site level.
除此之外,我会验证是否启用了 ViewState。可以在控件、页面甚至站点级别关闭 ViewState。
回答by HectorMac
It looks to me like you are creating a new eventhandler on each page load. This might be causing the problem. Why not attach the eventhandler declaratively:
在我看来,您正在为每个页面加载创建一个新的事件处理程序。这可能是导致问题的原因。为什么不以声明方式附加事件处理程序:
<asp:ListBox runat="server" id="lbox" autoPostBack="true" OnSelectedIndexChanged="lbox_SelectedIndexChanged" />
also, why not reference the control directly, instead of casting?
另外,为什么不直接引用控件,而不是强制转换?
protected void lbox_SelectedIndexChanged(object sender, EventArgs e)
{
int index = lbox.selectedIndex;
}
回答by Jennifer
Have you thought about loading the data earlier - e.g. in OnInit event on the page/user control. This occurs before the postback data is loaded and thus before an on-change can be processed? I believe that should work - but you might want to turn off viewstate!
您是否考虑过更早地加载数据 - 例如在页面/用户控件上的 OnInit 事件中。这发生在加载回发数据之前,因此在可以处理 on-change 之前?我相信这应该可行 - 但您可能想关闭视图状态!
回答by Rune
Works for me too. Does your foo() return the same values every time?
也适用于我。您的 foo() 是否每次都返回相同的值?
Just as a side note: if possible, you should really do your databinding in OnInit (every time, not just on GETs). If you do it before the call to base.OnInit(...), the contents of your listbox won't have to be serialized and deserialized to and from viewstate and sent across the wire to the client (yes, you will be hitting the database more, but you'll be hitting a system which is located on your local subnet, or even on the same machine. Moreover, the database will likely cache the result).
顺便提一下:如果可能的话,你真的应该在 OnInit 中进行数据绑定(每次,而不仅仅是在 GET 上)。如果您在调用 base.OnInit(...) 之前执行此操作,则列表框的内容将不必在视图状态之间进行序列化和反序列化,并通过线路发送到客户端(是的,您将数据库更多,但您将访问位于本地子网甚至同一台机器上的系统。此外,数据库可能会缓存结果)。
If you want to build high-performance websites, you need to take a close look at the way you use ViewState. I highly recommend this article: TRULY Understanding ViewState
如果你想构建高性能的网站,你需要仔细看看你使用ViewState的方式。我强烈推荐这篇文章:真正理解 ViewState
回答by FlySwat
Databinding DropDownLists/ListBoxes is painful, because they often bind to the wrong values.
数据绑定 DropDownLists/ListBoxes 很痛苦,因为它们经常绑定到错误的值。
I've given up on using DataBind(), and just resort to using a Foreach loop:
我已经放弃使用 DataBind(),转而使用 Foreach 循环:
foreach (Item i in DataSet)
{
listBox.Items.Add(etc);
}
回答by davidfowl
The realissue here is order of events. When you databind in page_load you overwrite the posted data, thats why the selection is not set in the listbox. You can easily overcome this by moving the binding logic to Page_Init.
这里真正的问题是事件的顺序。当您在 page_load 中进行数据绑定时,您会覆盖发布的数据,这就是未在列表框中设置选择的原因。您可以通过将绑定逻辑移动到 Page_Init 来轻松克服这个问题。
回答by Ben Lynch
Load the data in the Page_Init instead of the Page_Load. The data has to be filled during the Page_init to be available in the PostBack.
在 Page_Init 而不是 Page_Load 中加载数据。必须在 Page_init 期间填充数据才能在 PostBack 中使用。
回答by Mohit
if your listbox items are same then selected index will get set to 0.To rectify it, set different values to item.value
and let item.text
remains the same..then selected index will be displayed properly.
如果您的列表框项目相同,则所选索引将设置为 0。要纠正它,请将不同的值设置为item.value
并item.text
保持相同..然后所选索引将正确显示。