How do I switch visibility of a control in a repeater?

11,345

You need to get a reference to those controls and call set Visible property to false; something like this pseudo code;

ShoppingCartControlVariable.FinControl("idOfTheControlYouWantToHide").Visible=false;

See this documentation

Adding sample code to demonstrate how this is done:

Assuming you have a repeater like this (notice the OnItemCreated handler):

 <asp:Repeater ID="myrepeater" runat="server" OnItemCreated="myrepeater_ItemCreated">
        <HeaderTemplate>
            <table>
                <thead>
                    <th>
                        Link
                    </th>
                    <th>
                        Button
                    </th>
                </thead>
                <tbody>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td>
                    <asp:HyperLink ID="link" runat="server" Text='<%#Eval("Text")%>' NavigateUrl='<%#Eval("Url")%>'></asp:HyperLink>
                </td>
                <td>
                    <asp:Button ID="btnDelete" runat="server" Text="Delete" />
                </td>
            </tr>
        </ItemTemplate>
        <FooterTemplate>
            </tbody> </table>
        </FooterTemplate>
    </asp:Repeater>

You can hide/show elements in the repeater rows as follows:

protected void myrepeater_ItemCreated(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item && (boolean_condition_that_on_which_you_will_decide_what_to_show_and_what_to_hide))
    {
        e.Item.FindControl("link").Visible = false;
    }
}

For example, if I want to hide all link elements on every row and just leave the delete buttons, I can do this:

protected void myrepeater_ItemCreated(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item  || e.Item.ItemType==ListItemType.AlternatingItem)
    {
        e.Item.FindControl("btnDelete").Visible = false;
    }
}

And it will produce this: sample run

For reference, the code that I used to populate my repeater was this:

 List<CartItem> items = new List<CartItem>();
        for (int i = 0; i < 10; i++)
        {
            CartItem t = new CartItem();
            t.Text="Item " +i;
            t.Url="http://www."+i+".com";
            items.Add(t);
        }
        myrepeater.DataSource = items;
        myrepeater.DataBind();
Share:
11,345
ComfortablyNumb
Author by

ComfortablyNumb

Trying to learn c# (desperately!) so I'm sorry for asking dumb questions!

Updated on August 24, 2022

Comments

  • ComfortablyNumb
    ComfortablyNumb over 1 year

    I have a shopping cart that I am developing as a web user control. ucCart.ascx will appear on three different pages and I want the functionality of the cart to alter depending on which page it appears on. When the customer is confirming their order for example, I do not want to the delete cart item buttons or the recalculate cart button to be visible.

    Can this be done programmatically in code behind? I'd rather not use JavaScript. I naively tried to use cartDelete.Visible = false; but that's not liked at all!

  • ComfortablyNumb
    ComfortablyNumb over 12 years
    This works nicely outside the repeater (for say the 'confirm' button), but not inside the repeater.
  • Icarus
    Icarus over 12 years
    @ComfortablyNumb then what you need to do is handle the OnItemCreated event. I will post a sample project below in a minute.