Search code examples
c#asp.netdatabound-controls

Skipping Items During Data Binding


Greetings!

I have a Repeater control that's using an XmlDataSource control.

<asp:FormView id="myFormView" runat="server" DataSourceID="myXmlDataSource">
    <ItemTemplate>
            <span>Items</span>
        <asp:Repeater id="myRepeater1" runat="server" DataSource='<%# XPathSelect("Items/*")%>'>
            <HeaderTemplate>
                <ul>
            </HeaderTemplate>
            <ItemTemplate>
                <li><%# XPath("text()")%></li>
            </ItemTemplate>
            <FooterTemplate>
                </ul>            
            </FooterTemplate>
        </asp:Repeater>
        <span>Things</span>
        <asp:Repeater id="myRepeater2" runat="server" DataSource='<%# XPathSelect("Things/*")%>'>
            <HeaderTemplate>
                <ul>
            </HeaderTemplate>
            <ItemTemplate>
                <li><%# XPath("text()")%></li>
            </ItemTemplate>
            <FooterTemplate>
                </ul>            
            </FooterTemplate>
        </asp:Repeater>
    </ItemTemplate>
</asp:FormView>        
<asp:XmlDataSource ID="myXmlDataSource" XPath="Root" EnableCaching="false" runat="server" />

The XML looks something like this:

<Root>
    <Items>
        <Item>A</Item>
        <Item>B</Item>
        <Item>C</Item>
        <Item>D</Item>
    </Items>
    <Things>
        <Thing>1</Thing>
        <Thing>2</Thing>
        <Thing>3</Thing>
    </Things>
</Root>

In some cases, I'd like to "skip" the "B" item when binding so that it isn't displayed. Is there a way during the DataBinding event that I could skip the binding of the "B" item so that it won't be displayed?


Solution

  • You could investigate the current DataItem in the OnItemDataBound() method of the repeater and skip processing that item if it matches your criteria