One weekend later, thatβs what I came up with the solution. My main goal was to find something that would work and let you specify the exact contents of the element template in the markup. Doing things from code will work, but can still be cumbersome.
The code must be straightforward to follow, but the gist of the matter is in two parts.
The first is the use of the event generated by the Repeater element to filter out unwanted parts of the template.
The second is to store decisions made in ViewState to recreate the page during feedback. The latter is critical, as you will notice that I used Item.DataItem. During post backs, rest control occurs much earlier in the page life cycle. When the ItemCreate starts, the DataItem is null.
Here is my solution:
Control markup
<asp:Repeater ID="settingRepeater" runat="server" onitemcreated="settingRepeater_ItemCreated" > <ItemTemplate> <asp:PlaceHolder ID="text" runat="server"> <asp:Label ID="settingsLabel" CssClass="editlabel" Text='<%# XPath("@lbl") %>' runat="server" /> <asp:TextBox ID="settingsLabelText" runat="server" Text='<%# SettingsNode.SelectSingleNode(XPath("@xpath").ToString()).InnerText %>' Columns='<%# XmlUtils.OptReadInt((XmlNode)Page.GetDataItem(),"@width",20) %>' /> </asp:PlaceHolder> <asp:PlaceHolder ID="att_adder" runat="server"> <asp:CheckBox ID="settingsAttAdder" Text='<%# XPath("@lbl") %>' runat="server" Checked='<%# ((XmlElement)SettingsNode.SelectSingleNode(XPath("@xpath").ToString())).HasAttribute(XPath("@att").ToString()) %>' /> </asp:PlaceHolder> </ItemTemplate> </asp:Repeater>
Note. For added simplicity, I added a PlaceHolder control to group things together and decide which controls are easier to remove.
Code for
The code below is built on the notion that each relay element has a type. The type is retrieved from the xml configuration. In my specific scenario, I could make this type a single control using an identifier. If necessary, this can be easily changed.
protected List<string> repeaterItemTypes { get { List<string> ret = (List<string>)ViewState["repeaterItemTypes"]; if (ret == null) { ret = new List<string>(); ViewState["repeaterItemTypes"] = ret; } return ret; } } protected void settingRepeater_ItemCreated(object sender, RepeaterItemEventArgs e) { string type; if (e.Item.DataItem != null) {
source share