2012-07-11 109 views
0

我有以下代碼片段在運行時在列表視圖中獲取控件ID。但是,儘管提供了正確的ID,但在調試時ctrl仍爲空。在中繼器中獲取控制ID

foreach (ListViewItem item in lvData.Items) 
{ 
    string uid = item.ClientID; 
    Control ctrl = lvData.FindControl(uid+"_lbUnattend"); 
    ctrl.Visible = false; 
} 

我的模板:

<asp:ListView runat="server" ID="lvData" DataSourceID="odsEvents" 
     OnDataBound="lvData_DataBound" 
     onselectedindexchanged="lvData_SelectedIndexChanged"> 
     <LayoutTemplate> 
      <table class="diary" cellspacing="0" width="600px"> 
       <tr> 

        <th align="center"> 
         Date: 
        </th> 
        <th align="center"> 
         Time: 
        </th> 
        <th align="center"> 
         Event: 
        </th> 
        <th align="center"> 
         Location: 
        </th> 
        <th align="center"> 
        </th> 
       </tr> 
       <tr runat="server" id="itemPlaceHolder"> 
       </tr> 
      </table> 
     </LayoutTemplate> 
     <ItemTemplate> 
      <tr> 
       <td width="100px" align="center"> 
        <%#FRT.Utils.DateTimeHelper.FormatToShortDate(Eval("StartDate"))%> 
        <%#FRT.Utils.DateTimeHelper.FormatToShortDate(Eval("VisitDateStart"))%> 
       </td> 
       <td width="100px" align="center"> 
        <%#FRT.Utils.DateTimeHelper.FormatToShortTime(Eval("StartDate"))%> 
        <%#FRT.Utils.DateTimeHelper.FormatToShortTime(Eval("VisitDateStart"))%> 
       </td> 
       <td width="100px" align="center"> 
        <%#Eval("Title")%> 
       </td> 
       <td width="200px" align="center"> 
        <%# Eval("Address") %> 
       </td> 
       <td width="100px" align="center"> 
       <asp:LinkButton ID="lbLogin" runat="server" Text="I want to attend this event" CssClass="button" /> 
       <asp:LinkButton ID="lbAttend" runat="server" Text="I want to attend this event" CssClass="button" /> 
       <asp:LinkButton ID="lbUnattend" runat="server" CommandName="unattend" Text="I want to unregister from this event" CssClass="button" /> 
       <a href='<%# GetItemUrl(Eval("ActivityID"), Eval("SubEventID")) %>' class="button">See details</a>&nbsp 
       </td> 
      </tr> 
     </ItemTemplate> 
</asp:ListView> 

哪裏了LinkBut​​ton蒙山從lbUnattend生成的ID是我想要的人。

任何人指出問題出在哪裏?

+0

什麼情況下,你通過循環中的數據? – TheGeekYouNeed 2012-07-11 14:04:01

回答

2

我想你要找的是

foreach (ListViewItem item in lvData.Items) 
{ 
    Control ctrl = item.FindControl("lbUnattend"); 
    ctrl.Visible = false; 
} 
+0

+1您只能使用具有服務器ID的[FindControl](['ID'屬性](http://msdn.microsoft.com/en-us/library/system.web.ui.control.id.aspx ))而不是'ClientID'。 – 2012-07-11 14:04:10