当数据集中不存在 SelectedValue 时如何抑制 ArgumentOutOfRangeException
我有一个使用对象数据源来填充的下拉列表。返回的数据集是活动位置的列表。我们不删除数据,因此我们使用表中的 bDeleted 列进行软删除。
在某些情况下,填充 ddl 时,我在绑定 SelectedValue 时收到 ArgumentOutOfRangeException 错误,因为某个位置可能已被停用,并且未在列表中返回。
在我看来应该有一种方法来抑制这种情况,而只是不选择一个值。
我喜欢另一篇文章中给出的答案,建议手动添加红色条目,但我不确定在哪里/如何执行此操作。我是否需要在代码页上设置事件处理程序来捕获此错误。太晚了吗?
任何帮助/建议将不胜感激。
谢谢 帕特里克
I have a dropdownlist which uses a objectdatasource to populate. The dataset returned is a list of active locations. We do not delete data, so we do a soft delete using a bDeleted column in the table.
In some cases when populating the ddl, I get an ArgumentOutOfRangeException error when binding the SelectedValue because a location may have been deactivated, and is not returned in the list.
It seems to me there should be a way to supress this, and just not select a value.
I liked the answer given in another post suggesting adding an entry manually in red but I'm not sure where/how to do this. Do I need to setup an event handler on the code page to trap this error. Is that too late?
Any help/suggestions would be appreceiated.
Thanks
Patrick
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不认为抑制异常是一个好的做法。您始终可以在下拉列表中执行 FindByText 或 FindByValue 操作,如果存在,则仅设置选定的值。
或者
I don't think its a good practice to suppress exceptions. You can always do a FindByText or FindByValue on drop down and if it exists only set the selected value.
or
您应该避免有人“删除”仍在使用的位置或“删除”相关记录(将它们标记为已删除,如
bDeleted
)。您不应该使用异常来处理正常行为。
您应该更改 DropDownList 的 SelectCommand,以获取已“删除”但仍在某处使用的位置。然后您可以检查该位置是否已删除,并对已删除的 ListItems(Locations) 应用不同的 css-class:
DropDownList1.Items[1].Attributes.Add("class", "deleted");
You should have avoided that somebody can "delete" a location that is still in use or to "delete" the related records too(flag them as deleted like
bDeleted
).You shouldn't use exceptions to handle the normal behaviour.
You should change the SelectCommand for the DropDownList to get also the locations that are "deleted" but also still in use somewhere. Then you can check if the location is deleted and apply a different css-class on the deleted ListItems(Locations):
DropDownList1.Items[1].Attributes.Add("class", "deleted");