Dynamically add controls in an ASP.NET repeater

I find that I have a repeater control that binds to an XML document. My client now requests that the text field that is the repeater can be either a text box or a check box.

I cannot find an easy way to do the following:

if ((System.Xml.XmlNode)e.Item.DataItem.Attributes["type"] == "text")
<asp:TextBox runat="server" ID="txtField" Text='<%#((System.Xml.XmlNode)Container.DataItem).InnerText %>' CssClass="std"></asp:TextBox>
else
<asp:CheckBox runat="server" ID="txtField" Text='<%#((System.Xml.XmlNode)Container.DataItem).InnerText %>' CssClass="std"></asp:TextBox>

Is there a good way to extend the current implementation without rewriting the logic. If I could insert a control through "OnItemDataBound", that would also be right. But I can’t make it work.

+5
source share
3 answers

How about something similar in your markup in each text box and checkboxes?

Visible=<%= Eval("type").tostring() == "text") %>
+3

, TextBox CheckBox Panel. DataItem , , ​​ "" , css ..

+5

mspmsp sugestion. :

aspx:

<asp:Repeater ID="myRepeater" runat="server" OnItemCreated="myRepeater_ItemCreated">
    <ItemTemplate>
        <asp:PlaceHolder ID="myPlaceHolder1" runat="server"></asp:PlaceHolder>
        <br />
    </ItemTemplate>
</asp:Repeater>

:

dim plh as placeholder
dim uc as usercontrol
protected sub myRepeater_ItemCreated(object sender, RepeaterItemEventArgs e)
    if TypeOf e Is ListItemType.Item Or TypeOf e Is ListItemType.AlternatingItem Then
        plh = ctype(e.item.findcontrol("myPlaceHolder1"), Placeholder)
        uc = Page.LoadControl("~/usercontrols/myUserControl.ascx")
        plh.controls.add(uc)
    end if
end sub
+3

All Articles