开发者

Hide items from a ListView control

开发者 https://www.devze.com 2023-01-10 04:03 出处:网络
I am using a listview to display some items.But sometimes based on condition开发者_运维百科 I have to hide few items from the list.So how can I do this?I am using ASP.Net with c#.There is no way to \"

I am using a listview to display some items.But sometimes based on condition开发者_运维百科 I have to hide few items from the list.So how can I do this?I am using ASP.Net with c#.


There is no way to "hide." You will have to remove and then add when you want the item to be visible again.


Options include:

  • If you're talking about hiding an entire item in the list... Writing your query so those items are filtered out when you get the list that your ListView is bound to. (i.e. add a WHERE clause to the SELECT statement if these are coming from the DB.)
  • If you're talking about specific controls within an ItemTemplate in your ListViews, you can set the "Visible=false" in places where you want it hidden.

like so:

<ItemTemplate>
   <asp:Button Runat = "Server" visible="<%# Eval(SomeCondition) %>" Text = "Click Me" />
</ItemTemplate>


If your list items are dynamically populated, I would check for the condition, and then chance the DataSourceID, or the query that the datasource uses and then

MyListView.DataBind();

Otherwise, if it is not dynamically populated, you could define the ListItems as static members of that page, then check for your condition and remove the items that you want to "hide" before you add the ListItems collection to the ListView.


I've just found a solution to solve this problem, using ItemsContainerStyle:

<ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
        <Setter Property="Visibility" Value="{Binding propertyName, Converter={StaticResource ITVConverter}}"/>
    </Style>
</ListView.ItemContainerStyle>


Set the width property to 0 in the listview item.

Hide items from a ListView control

0

精彩评论

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

关注公众号