|
|||||||||||||||||||
|
|
The IRepeatInfoUser Interface... The IRepeatInfoUser interface is a helpful interface for generating repetitive information in a tabular way... By: Brian Mains The IRepeatInfoUser interface is a helpful interface for generating repetitive information in a tabular way. You've seen this functionality with the checkboxlist control, where you can change the RepeatDirection and RepeatColumns to create a directional or tabular layout. It gives the developer more capabilities when designing web pages to change the layout rendered for the control, and is actually easy to do. When implementing IRepeatInfoUser, the following properties/methods are added to your control, which are:
In addition to writing code for these properties/methods, we will need to add three more custom properties in our control, which are the Count, Direction, and Layout properties. These properties affect how the control is rendered, are used at Rendering time, and are not implemented by the interface. The first one is of type integer and the last two are enumerations, as declared below:
Let me explain RenderItem first before explaining the properties. RenderItem fires once for every item in the target collection. The method has a ListItemType enumeration that determines where the control is in the rendering. So, it has an enumeration value for the Header, Footer, Item, AlternatingItem, Separator, etc. So when it runs, RenderItem fires for the Header, then Item, then Separator, then AlternatingItem, and to Footer, leaving out the other enumerated types. Let's look at the first three implemented properties, which affect how many times RenderItem fires. HasHeader, HasFooter, and HasSeparator determine whether the RenderItem method fires for the Header, Footer, and Separator sections of the control. Based on the boolean value returned, the control will render those items; the problem with the properties is that they are readonly, and thus the ability to render them must be determined from other properties, the existence of a template, etc. I also added the Browsable attribute to hide them from the PropertyGrid. If you notice, when you select the control in the ASPX page, you can't see these properties; that's the function of the Browsable attribute.
So, these properties turn on/off certain sections of the control. GetItemStyle, on the other hand, returns the appropriate style for the control based on the same enumeration. GetItemStyle has all the necessary information to make that determination based on the enumeration and the index of the control, which your code will return the appropriate style.
Now to the code of RenderItem; RenderItem renders the HTML for the item, using the HTMLTextWriter object. Alternatively, you can create server controls and invoke the RenderControl method of this control to render the HTML to the writer, which is a better approach for more complex controls.
So how does the control know how many items to render? That is handled by the RepeatedItemsCount readonly property, which returns the count from the collection.
Lastly, how is it rendered? Render instantiates a RepeatInfo object with all of the required information, and makes a call to RenderRepeater, which will fire the RenderItem method and pass in all the necessary information for you.
IRepeatInfoUser is a handy interface and works well with your custom controls in several situations, especially for list controls who want to render in a table structure. It is an excellent interface to consider when designing custom controls. The example control provided, NewsRepeater, inherits the list from NewsControl class, which implements a custom list property named Entries. It renders entries in a table format as defined by the control developer. For this example, only the default.aspx and the NewsRepeater.cs class are used; all others can be ignored. |
|
|||||||||||||||||
|
|||||||||||||||||||