开发者

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

开发者 https://www.devze.com 2023-03-26 03:08 出处:网络
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

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 b开发者_如何学Pythonehind? I'd rather not use JavaScript. I naively tried to use cartDelete.Visible = false; but that's not liked at all!


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:

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

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();


I think you can make a public function inside this user control named "hide controls" and call this function in the page you want and this function hide the controls or you can make a boolean property in the user control and the page can set it to false and then you can use this flag to hide the controls inside your usercontrol.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号