如何隐藏数据列表中的项目 [英] How to hide an item in datalist
问题描述
我想根据一些条件起诉 ItemBound
,怎么样?
I want to hide an item in datalist
according to some condition suing ItemBound
, how ?
推荐答案
环绕式的整个内容占位符
控制的ItemTemplate
。
Wrap a PlaceHolder
control around the entire content of the ItemTemplate
.
然后在你的ItemDataBound事件,你可以这样做:
Then in your ItemDataBound event, you could do something like:
Protected Sub myDataList_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.DataListItemEventArgs) Handles myDataList.ItemDataBound
If Not Value = Value2 Then
Ctype(e.Item.FindControl("myPlaceHolder"), PlaceHolder).Visible = False
End If
End Sub
有一个更好的方法(但是我已经没有机会来测试它),将使用 e.Item.Visible
来隐藏整个项目。这样没有HTML表格元素将呈现该项目。这也意味着没有占位符
将不得不以复加。
A better approach (however I've not had chance to test it), would be to hide the whole item using e.Item.Visible
. This way no HTML table elements would be rendered for the item. It would also mean no PlaceHolder
would have to be added.
Protected Sub myDataList_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.DataListItemEventArgs) Handles myDataList.ItemDataBound
If Not Value = Value2 Then
e.Item.Visible = False
End If
End Sub
另外,如果您正在检查的值是从数据库源,可以筛选出的项目绑定之前:
Alternatively, if the values you are checking are from a database source, you could filter the items out before binding:
WHERE Value=@Value2
这篇关于如何隐藏数据列表中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!