2010-05-19 82 views
1

嗨有人可以告訴我如何隱藏DataList中的LinkBut​​ton?在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; 

         } 

        } 
       } 

      } 
     } 
    } 

謝謝!

回答

1

試試這個:

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

您應該將此代碼移到

protected virtual void OnItemDataBound(
    DataListItemEventArgs e 
) 

事件。在這種情況下,你應該使用e.Item.FindControl('LinkButtonID')方法爲尋找你的控制

更多信息是here

相關問題