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#.
+1
A:
There is no way to "hide." You will have to remove and then add when you want the item to be visible again.
Lucas B
2010-08-05 14:42:31
Not true... You can set Visible = False on the items within the ListView based on criteria.
David Stratton
2010-08-05 14:50:05
@David I think he means dynamically hiding items, not based on criteria, but based on events. That is, he wants to be able to do this: this.listView1.Items[0].Visible = false; Which he can't do.
Lucas B
2010-08-05 16:07:34
If that's the case, then it can be done in the ItemCommand event. (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.itemcommand.aspx) Almost any visible control that's set to run at the server (via Runat="Server" inthe tag) can have the visibility set to false. I would likely do it with the "Select" command.
David Stratton
2010-08-05 16:18:26
Not to mention if he can get the generated ID for the control, he can set the item.style.display = "none" via javascript...
David Stratton
2010-08-05 16:19:11
A:
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.
Lloyd
2010-08-05 14:45:22
A:
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>
David Stratton
2010-08-05 14:47:51