Editable Repeater
For a project at work I needed a repeater with support for an additional template, namely one for editing an item. You have this functionality in the DataList and DataGrid built-in controls, but those don’t offer the same layout flexibility the Repeater has (something which I like very much: I hardly use DataList or DataGrid).
An so I wrote the EditableRepeater class. It’s actually very simpel: it defines an EditTemplate property in which you can specify the template to display when you want to “edit” an item. There’s also an EditItemIndex property to define the index of the item you want the edit template to display for. The rest (saving state, invoking the edit template by setting the index property, etc) is up to you. I did not provide an EditCommand event like in DataList for example, but since that’s a specific case of an ItemCommand, that doesn’t hurt the provided functionality.
Here’s a video of the repeater in action:
Editable Repeater Example from Tom Adriaenssen on Vimeo.
The same action as with a DataList, but now with the freedom of a Repeater.
You can download the C# code: it’s not large since it builds on the Repeater control which handles most of the hard stuff. All this does is add another Template, define an EditItemIndex property and provides some glue to make the magic happen.
To use it, wrap this in an assembly and register it in web.config:
<system.web> <pages> <controls> <add tagPrefix="iwc" namespace="Inferis.Web.Controls" assembly="Inferis.Web.Controls" /> </controls> </pages></system.web>
And then use the new control as you would use the normal Repeater:
<iwc:EditableRepeater ID=”Test” runat=”server” EditItemIndex="1"> <ItemTemplate> <p> This is the Item Template for <% Eval("FieldName") %>. </p> </ItemTemplate> <EditorTemplate> <p> Editing an item: <asp:TextBox Id="EditBox" Text='<% Eval("FieldName") %>' runat="server" /> </p> </EditorTemplate></iwc:EditableRepeater>
Notice the EditorTemplate to define the editing template, and the use of EditItemIndex to mark row 1 as editable. Of course, you’ll need to do some programming to do this dynamically, but the same principles as using an EditTemplate in a DataList or DataGrid count, so I’m not going to explain that further.
And that’s about it. It also works charmly with the ASP.Net Ajax Extensions.


