Repeater Control - Unlink for a specific item

Inside the repeater control, is there a way to de-bind certain elements to display the page?

We currently have a set of elements associated with the repeater, and if the element is not part of the current language, we hide the element.

I want to be able to make a count on the repeater and return a real number. An account that also does not include hidden items.

Is it possible to unbind certain elements, possibly in the ItemDataBound event?

Update

For each item in the collection that we are linking, we check the database during ItemDataBound to obtain additional information about the item, such as language, etc. We are currently stopping us from filtering related data before linking it.

+7
binding repeater
source share
4 answers

I agree with the other answers - the best solution (for both performance and code clarity) is to reverse engineer the page so that you can filter out invalid entries before data binding.

Most data sources do not allow us to delete their elements, while ASP.NET iterates. For example, if you bind to a simple generic List<T> , and you delete an item during iteration, the list will InvalidOperationException .

In other cases, ASP.NET actually iterates over a copy of the data source. If you bind to a DataTable , ASP.NET uses a copy of the content (by default the DataView), rather than iterating the source rows themselves - you can delete items from the underlying data source during the iteration, but this does not affect the data binding operations.

If filtering items in advance is not an option, your current solution is fine: just hide the items! If you need to correctly count the counter, track the number of invalid elements in the ItemDataBound handler and show it as a page-level property:

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

A more suitable solution would be to filter the associated collection if there is no specific need for these hidden elements. Something like

 items.Where(i => i.IsInLanguage(currentLanguage)); 

Update:

As for me, I would use this approach:

 var items = db. Where(i => i.IsInLanguage(currentLanguage)). Where(i => i.SomeField == anotherFilterParameter); repeater.DataSource = items; repeater.DataBind(); 

So, all filtering is applied in advance

It will also reduce the number of return flights to a database that plays better performance.

+3
source share

Why not filter the data source before binding. Suppose you use some custom objects:

 myRepeater.DataSource=repository.getItems().Where(item=>item.Language==CurrentLanguage); 

If you do not need them, do not bind them first.

Update

If at all possible, you should thoroughly extract this information from the database. Are these lists large? If so hit db once for each item in the list will appear as a performance issue.

+2
source share

The answer is very simple, you just set the Visible property to false for the element and it will not be displayed. In this example, I remove items from the list of products that are only available to new customers if the current user has a purchase history:

 void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (!userHasPurchaseHistory) { return; } // filter out products only allowed for new members if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { System.Data.Common.DbDataRecord rec = (System.Data.Common.DbDataRecord)e.Item.DataItem; if (rec != null) { bool newMemberOnly = Convert.ToBoolean(rec["NewMemberOnly"]); if (newMemberOnly) { e.Item.Visible = false; } } } } 

Please note that the above is binding to binding to IDataReader , you may need to drop e.Item.DataItem to another object depending on what you are binding to.

Note that I will definitely never perform another database search during the binding, you should never access the database in a loop, but for now, the data you are binding has something that you can check to decide whether you want to show it there is nothing wrong with filtering in ItemDataBound . This can be problematic if you are doing any paging, as this will make the page sizes inconsistent.

+2
source share

All Articles