有人可以告诉我如何在DataList中隐藏LinkButton吗?
我试过这样做,但我不工作:
protected void Page_PreRender(object sender, EventArgs e)
{
foreach (var item in listanews)
{
DataList container = dlgestionenews;
if (string.IsNullOrEmpty(item.IdNews))
{
DataListItem itemdatalist = null;
foreach (DataListItem itemdl in container.Items)
{
foreach (Control control in itemdatalist.Controls)
{
if (control.GetType().FullName == "LinkButton")
{
((LinkButton)control).Visible = false;
}
}
}
}
}
}
谢谢!
答案 0 :(得分:1)
试试这个:
foreach (DataListItem dli in yourDataListControl.Items)
{
LinkButton lbLinkButton = (LinkButton)dli.FindControl("yourLinkButtonID");
if (lbLinkButton != null)
{
lbLinkButton.Visible = false;
}
}
答案 1 :(得分:0)
您应该将此代码移至
protected virtual void OnItemDataBound(
DataListItemEventArgs e
)
事件。在这种情况下,您应该使用e.Item.FindControl('LinkButtonID')
方法来查找控件
更多信息是here