即使没有选择索引,DropDownList上的DataBind也会抛出异常(DataBind on a DropDownList throws exception even though no ind

编程入门 行业动态 更新时间:2024-10-23 17:23:14
即使没有选择索引,DropDownList上的DataBind也会抛出异常(DataBind on a DropDownList throws exception even though no index is selected)

所以我收到了这个错误

{"'drpButton1' has a SelectedValue which is invalid because it does not exist in the list of items.\r\nParameter name: value"}

从我能读到的一切,这是因为DropDownList要么具有现有项目,要么具有选定的索引或值,而不是新的数据绑定项目。

但问题是我可以保证对象中没有现有项目,我也可以放心地说,没有选择可能超出范围的索引。

这是DropDownList对象在.databind()调用之前直接看起来的样子。

这是直接在引发所有爆炸的databind()调用之后。

我的列表对象包含7个项目,特别是它包含数据绑定方法随机决定选择的项目。

但是这里是踢球者我用完全相同的数据填写了8个下拉列表并且它在第一个下拉列表中工作就好了。 不知道为什么第二个爆炸。

编辑:这是执行绑定的代码:

这是load方法的一个片段。 第一个调用成功,第二个调用失败,但它不会一直失败。

private void LoadShortCodeDropDownData() { // Initilization junk to get the resultList to use. base.LoadListDropDown(drpButton0, (IList)resultList, "DeviceShortCodeIndexID", "DeviceShortCodeName", select); MessageTextEnabled(drpButton0); base.LoadListDropDown(drpButton1, (IList)resultList, "DeviceShortCodeIndexID", "DeviceShortCodeName", select); MessageTextEnabled(drpButton1); } protected void LoadListDropDown(DropDownList dropDown, IList list, string valueField, string textField, string insertItem) { LoadListDropDown(dropDown, list, valueField, textField); //dropDown.Items.Insert(0, new ListItem(insertItem, "")); } protected void LoadListDropDown(DropDownList dropDown, IList list, string valueField, string textField) { dropDown.DataSource = list; dropDown.DataValueField = valueField; dropDown.DataTextField = textField; dropDown.DataBind(); }

编辑2:我认为我在这里的真正问题是数据绑定选择哪个项目选择? 我注意到第一个获取数据绑定的下拉列表会随机选择列表中的第一个值,而第二个下拉列表由于某种原因尝试绑定到列表中的最后一个值。

So i am getting this error

{"'drpButton1' has a SelectedValue which is invalid because it does not exist in the list of items.\r\nParameter name: value"}

From everything i can read this is because the DropDownList either had existing items or had a selected index or value which is not in the new databound items.

But the thing is i can GUARANTEE that there are no existing items in the object and i can also say with confidence that there has not been an index selected that could be out of range.

Here is what the DropDownList object looks like directly before the .databind() call.

Here it is directly after the databind() call that has caused all the explosions.

My list object contains 7 items and in particular it contains the item that the databind method randomly decides to pick.

But here is the kicker i literally fill up 8 dropdowns with the EXACT same data and it works on the first dropdown just fine. No idea why the second one explodes.

EDIT: here is the code that does the binding:

Here is a snippet from the load method. the first call succeeds the second one fails but it doesn't fail all the time.

private void LoadShortCodeDropDownData() { // Initilization junk to get the resultList to use. base.LoadListDropDown(drpButton0, (IList)resultList, "DeviceShortCodeIndexID", "DeviceShortCodeName", select); MessageTextEnabled(drpButton0); base.LoadListDropDown(drpButton1, (IList)resultList, "DeviceShortCodeIndexID", "DeviceShortCodeName", select); MessageTextEnabled(drpButton1); } protected void LoadListDropDown(DropDownList dropDown, IList list, string valueField, string textField, string insertItem) { LoadListDropDown(dropDown, list, valueField, textField); //dropDown.Items.Insert(0, new ListItem(insertItem, "")); } protected void LoadListDropDown(DropDownList dropDown, IList list, string valueField, string textField) { dropDown.DataSource = list; dropDown.DataValueField = valueField; dropDown.DataTextField = textField; dropDown.DataBind(); }

EDIT2: I think the real question i have here is how is the databind picking which item to select? I noticed that the first dropdown that gets databound randomly selects the first value in the list while the second one tries to bind to the very last one in the list for some reason.

最满意答案

这是在第一次加载页面时还是在回发后发生的? 因为如果它是一个回发,你很可能只是默认情况下SelectedIndex == 0 。

我不能保证这会解决问题,但你可以尝试添加

dropDown.SelectedIndex = -1;

...到第二个LoadListDropDown重载的顶部。

Is this happening on the first load of the page, or after a postback? Because if it's a postback, you very well could have SelectedIndex == 0 just by default.

I can't guarantee that this will solve the problem, but you could try adding

dropDown.SelectedIndex = -1;

... to the top of your second LoadListDropDown overload.

更多推荐

本文发布于:2023-07-31 20:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1347367.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:也会   抛出   索引   异常   DataBind

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!