中继器控制-取消绑定特定项目 [英] Repeater control - Cancel bind for specific item

查看:84
本文介绍了中继器控制-取消绑定特定项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在转发器控件中,是否有一种方法可以在呈现页面之前解除某些项目的绑定?

Within a repeater control, is there a way of de-binding certain items before the page is rendered?

当前,我们有一组绑定到转发器的项目,如果该项目不是当前语言的一部分,我们将隐藏该项目.

Currently we have a collection of items being bound to a repeater and if the item is not part of the current language, we hide the item.

我希望能够对转发器进行计数并获得有效的号码.不包含隐藏项的计数.

I'm wanting to be able to do a count on the repeater and get a valid number back. A count that doesn't also include the hidden items.

是否可能在ItemDataBound事件中取消绑定特定项目?

Is it possible to de-bind specific items perhaps in the ItemDataBound event?

更新

对于要绑定的集合中的每个项目,我们在ItemDataBound期间检查数据库以获取有关该项目的更多信息,例如语言等.这目前使我们无法在绑定数据之前对绑定的数据进行过滤./p>

For each item in the collection we're binding, we check the database during the ItemDataBound for further information about the item, such as language etc. This is currently stopping us from filtering the bound data before binding it.

推荐答案

我同意其他答案-最佳解决方案(为了提高性能和代码清晰度)是重新设计页面,以便您可以过滤无效的条目数据绑定之前.

I agree with the others answers - the best solution (for both performance and code clarity) is to redesign the page so that you can filter invalid entries out before databinding.

大多数数据源不允许我们在ASP.NET迭代它们时删除它们的项.例如,如果您绑定到简单的通用List<T>,并且在迭代时删除了某个项目,则列表将抛出InvalidOperationException.

Most data sources don't allow us to remove their items while ASP.NET is iterating them. For example, if you bind to a simple generic List<T>, and you remove an item while iterating it, the list will throw an InvalidOperationException.

在其他情况下,ASP.NET实际上会迭代数据源的副本.如果绑定到DataTable,则ASP.NET使用内容的副本(默认为DataView)而不是迭代源行本身-您可以在迭代时从基础数据源中删除项目,但不会影响数据绑定操作.

In other cases, ASP.NET actually iterates a a copy of the data source. If you bind to a DataTable, ASP.NET uses a copy of the contents (the default DataView) rather than iterating the source rows themselves - you can remove items from the underlying data source while iterating, but it doesn't affect the databinding operation.

如果真的不能预先过滤项目,那么您当前的解决方案就可以了:只需隐藏项目即可!如果您还需要获取正确的计数,请在ItemDataBound处理程序中跟踪无效项的数量,并将其显示为页面级属性:

If filtering the items in advance really isn't an option, your current solution is fine: just hide the items! If you need to get the correct count on top of that, track the number of invalid items in your ItemDataBound handler and expose it as a page-level property:

if (IsInvalid(args.Item.DataItem)) {
    this.invalidItemCount++;
    // code to hide the current item
}

这篇关于中继器控制-取消绑定特定项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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