在转发器中获取控制ID

时间:2012-07-11 13:59:17

标签: c# asp.net .net repeater uniqueidentifier

我有以下代码片段在运行时在listview中获取控件ID。但是,尽管提供了正确的id,但在调试时ctrl为null。

 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>

其中从lbUnattend生成的链接按钮是我想要的。

有人指出问题出在哪里?

1 个答案:

答案 0 :(得分:2)

我认为您正在寻找的是

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