views:

55

answers:

2

Hi someone can tell me how to hide a LinkButton inside a DataList?

I've tried to do this but I do not work:

 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;

                        }

                    }
                }

            }
        }
    }

Thanks!

A: 

You should move this code to the

protected virtual void OnItemDataBound(
    DataListItemEventArgs e
)

event. In this event, you should use the e.Item.FindControl('LinkButtonID') method for the finding your control

More info is here

VMAtm
A: 

Try this:

foreach (DataListItem dli in yourDataListControl.Items)
{
    LinkButton lbLinkButton = (LinkButton)dli.FindControl("yourLinkButtonID");
    if (lbLinkButton != null)
    {
         lbLinkButton.Visible = false;
    }
}
dejanb