 |
|
 |
This will simply use reflection on the binding object, so it will have to have a property, but if you need to change that, you can simply add another property to the class that you can set in the HTML designer that the OnItemCreated code can use:
using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
public class GroupingRepeater : Repeater
{
private string _lastGroup;
private string _groupColumn;
private ITemplate _groupTemplate;
[Browsable(true), Category("Behaviour")]
public string GroupColumn
{
get { return _groupColumn; }
set { _groupColumn = value; }
}
[PersistenceMode(PersistenceMode.InnerProperty)]
[Browsable(false)]
[TemplateContainer(typeof(RepeaterItem))]
[DefaultValue("")]
public virtual ITemplate GroupTemplate
{
get { return _groupTemplate; }
set { _groupTemplate = value; }
}
public GroupingRepeater()
{
_lastGroup = null;
}
protected override void OnItemCreated(RepeaterItemEventArgs e)
{
base.OnItemCreated(e);
if (!string.IsNullOrEmpty(GroupColumn))
{
string currentGroup = e.Item.DataItem.GetType().GetProperty(GroupColumn).GetValue(e.Item.DataItem, null).ToString();
if (_lastGroup == null || _lastGroup != currentGroup)
{
RepeaterItem item = new RepeaterItem(0, ListItemType.Item);
GroupTemplate.InstantiateIn(item);
item.DataItem = e.Item.DataItem;
this.Controls.Add(item);
item.DataBind();
_lastGroup = currentGroup;
}
}
}
}
Example HTML to setup:
<mkc:GroupingRepeater ID="rptLinks" GroupColumn="LinkCategory" runat="server">
<GroupTemplate>
<div><%# Eval("LinkCategory") %></div>
</GroupTemplate>
<ItemTemplate>
<a href="<%# Eval("Link") %>" target="_blank"><%# Eval("Title") %></a><br />
</ItemTemplate>
</mkc:GroupingRepeater>modified on Monday, February 15, 2010 2:23 PM
|
|
|
|
 |
|
 |
Thanks so much for posting your version!
I have a minor improvement to recommend. If you want to be able to traverse an entity, swap out the following line:
string currentGroup = DataBinder.Eval(e.Item.DataItem, GroupColumn).ToString();
This allows you to do something like this:
<cust:GroupingRepeater ID="RequirementsList" GroupColumn="Object.Item.AnotherItem" runat="server" OnItemCreated="RequirementsList_ItemCreated">
|
|
|
|
 |
|
 |
I have an XML file that has a category column. I want to be able to group by this category, but it my categories are not listed together the group by doesn't seem to work.
Here is my XML file:
<?xml version="1.0" standalone="yes"?>
<projects>
<detail>
<category>Office</category>
<id>1</id>
</detail>
<detail>
<category>Office</category>
<id>2</id>
</detail>
<detail>
<category>Industrial</category>
<id>3</id>
</detail>
<detail>
<category>Office</category>
<id>4</id>
</detail>
<detail>
<category>Mall</category>
<id>5</id>
</detail>
</projects>
When I run the code I see the following:
Office
1
2
Industrial
3
Office
4
Mall
5
I want the display to look like:
Office
1
2
4
Industrial
3
Mall
5
Your help would be greatly appreciated.
If I am not clear please feel free to ask for more clarification.
Thanks in advance
|
|
|
|
 |
|
 |
I think you'll have to sort it before hand. Either that, or you have to override the Datasource property and take care of it there. You could add a property to the class that will tell which "column" to sort on.
|
|
|
|
 |
|
 |
Control means you just drag & drop with .dll in your bin. But here you need to implement class.
But idea is nice. Appreciate.
|
|
|
|
 |
|
 |
act i wld like to know how to format the rows of the repeater control in asp.net where the rows of the table are dispalyed as list.. and also i need to hide certain columns when the particular row does not contain any data...........
plz its urgent could any1 help me a round.........
|
|
|
|
 |
|
 |
The lastvalue property is compared against the current dataitem during databind, and used to determine whether a group header/footer is required. Unfortunately, while it is set to null at CreateChildControls(), this does not guarantee that lastvalue is null if and when a rebind occurs. If the repeater is rebound after the initial databind, an extra group footer appears and child control numbering in the repeater gets out of whack. This in turn messes with the repeater's ItemCommand event such that ItemCommand event does not fire when a child control is clicked.
My fix: set lastvalue to null ondatabinding, e.g.
public GroupingRepeater()
{
ItemCreated += GroupingRepeater_ItemCreated;
DataBinding += GroupingRepeater_DataBinding;
}
private void GroupingRepeater_DataBinding(object sender, EventArgs e)
{
lastvalue = null;
}
|
|
|
|
 |
|
 |
While you are exactly right, this bug only shows if you rebind the contents twice in a single postback, right? Anyway, I would suggest to overload the DataBind() method like so:
public override DataBind()
{
lastvalue = null;
base.DataBind();
}
Thanks for your insight.
|
|
|
|
 |
|
 |
I would suggest to update code in article, because not everybody reads all the comments before using code
|
|
|
|
 |
|
 |
your code sample declares GroupingRepeater as
public class GroupingRepeater : System.Web.UI.Control, INamingContainer
while this page declares it as
public class GroupingRepeater : System.Web.UI.WebControls.Repeater
why is that so ?
|
|
|
|
 |
|
 |
Hi, couple questions:
With GroupingRepeater : System.Web.UI.Control, INamingContainer:
- How do you implement the Items property of GroupingRepeater?
- Does it have to inherit from ICollection and IEnumerable also?
- What needs to be implemented in order to make it a repeater replacement?
Thanks
|
|
|
|
 |
|
 |
This is exactly what I've been looking forn unfortunately I cant seem to get it working. I am new to ASP.Net and have a simple five column repeater control that I want grouped by the second column. I just cannot figure out how to make the download work as an example.
I am trying to copy and paste the code behind the forms and I get compile errors. Can someone help me with this?
|
|
|
|
 |
|
 |
Does anyone have this control as a compiled DLL. I'm unable to compile it using csc and do not use Visual Studio for my website.
|
|
|
|
 |
|
 |
I'm having trouble making this work. The "downloadable" demo code is not complete. It only has Assembley.Info and an empty bin folder. Am I missing something? This control looks great, and I'm sure allot of the problem is my lack of experience.
David W. Hainsworth
|
|
|
|
 |
|
 |
Items.Count does not appear to work inside this control.
For instance, say I have a recordset with 30 items in it and that I bind this data to the GroupingRepeater control.
Also assume that four of these items will be grabbed as GroupHeader items via a customer Comparer object.
This makes for 30 records, plus 4 GroupHeader items, making 34 total items.
However, if you call the Items.Count property then it returns a value of 30. This 30, when looped through refers to the first 30 items, INCLUDING the GroupHeader items. Which results in the last 4 records magically inaccessible.
Any thoughts on how to fix this?
|
|
|
|
 |
|
 |
I'm aware of this problem. My approach to a grouping repeater does have some terrible side effects, and this is one of them. It has something to do with me inserting additional items on the fly. This is also shown when you use controls in the templates that generate postback events.
I do not see a solution how to fix this, other than developing an entirely different control.
|
|
|
|
 |
|
 |
I've uploaded the new code, but I forgot to check this bug!
|
|
|
|
 |
|
 |
It seems that on each Postback (if I dont't rebind the GroupedRepeater) al GroupHeader-Items get normal Repeater Items.
Does anyone have a solution to this already?
|
|
|
|
 |
|
 |
I have an updated version of this control which does not have this bug.. Send me a note when you're interested.
|
|
|
|
 |
|
 |
Although the article text hasn't changed, I've updated the download so that this bug is fixed! Good luck!
|
|
|
|
 |
|
 |
Scenario:
a blog application with items grouped by day, every item with a linkbutton, the grouprepeater element with onItemCommand event handler to fires the linkbuttons click.
Only the pair groups (the 2nd, the 4th, the 6th and so on) items event are fired.
Repeater output example:
--day1--
item1 (with linkbutton)
item2 (with linkbutton)
--day2--
item1 (with linkbutton)
item2 (with linkbutton)
--day3--
item1 (with linkbutton)
item2 (with linkbutton)
--day4--
item1 (with linkbutton)
item2 (with linkbutton)
Only day2 and day4 item's linkbuttons are fired in the itemCommand eventhandler.
What's wrong?
|
|
|
|
 |
|
 |
To be honest, I haven't actually tried this control with postbacking controls myself.. Could you please send me some of your code so I can test it out?
|
|
|
|
 |
|
 |
I've found another solution and change some code, now I've tried to re-create the problem without success...
Probably my mistake
Joe
|
|
|
|
 |
|
 |
Could you enlighten me with your solution?
|
|
|
|
 |
|
 |
I am also seeing some problem with onItemCommand not firing for every item, it seems to fire for some but not others, sometimes two out of a group of three will work, other times, a group with a single item will not fire the event, but the postback occurs..
any ideas on why this is occuring?
thanks
|
|
|
|
 |