在DataList中隐藏LinkBut​​ton

时间:2010-05-19 11:59:26

标签: asp.net asp.net-3.5

有人可以告诉我如何在DataList中隐藏LinkBut​​ton吗?

我试过这样做,但我不工作:

 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;

                        }

                    }
                }

            }
        }
    }

谢谢!

2 个答案:

答案 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