无法在Code Behind中引用PlaceHolder

时间:2012-10-23 20:15:58

标签: c# asp.net placeholder

我正在构建一个包含转发器的ASP.NET用户控件。在ItemTemplate中,我删除了一个PlaceHolder。每当数据源中的布尔字段为真时,我都试图在Place Holder中显示Group Header。

Repeater工作正常,加载并显示除Group Header之外的所有数据。但是,Visual Studio不会让我在后面的代码中引用PlaceHolder。错误消息是“名称'PlaceHolder1'在当前上下文中不存在”。我查看了其他类似的问题并尝试使用FindControl,但它返回null。

    <%@ Control Language="C#" ClassName="Table1Control" AutoEventWireup="true"         CodeBehind="Table2View.ascx.cs" Inherits="CCC.Table2View" %>

        <table>
        <asp:Repeater ID="Table2List" runat="server" onitemdatabound="Table2List_ItemDataBound">
    <HeaderTemplate>

            <tr class="Table2HeaderRow">
                <th class = "Table2Col1Hdr">Test/Report Group</th>
                <th class = "Table2Col2Hdr">Agent</th>
                <th class = "Table2Col3Hdr">Disk Content</th>
                <th class = "Table2CriteriaSetHdr">
                    <table class="Table2CriteriaSetTable">
                        <tr class="Table2CriteriaSetInnerRow">
                            <td class = "Table2CriteriaSetTopHdr" colspan=3>Zone Diameter </td></tr>
                        <tr class="Table2CriteriaSetInnerRow">
                            <td class="Table2CriteriaSetSHdr">S</td>
                            <td class="Table2CriteriaSetIHdr">I</td>
                            <td class="Table2CriteriaSetRHdr">R</td>
                        </tr>
                    </table>
                </th>
                <th class = "Table2CriteriaSetHdr">
                    <table class="Table2CriteriaSetTable">
                        <tr class="Table2CriteriaSetInnerRow">
                            <td class = "Table2CriteriaSetTopHdr" colspan=3>MIC Interperetive </td></tr>
                        <tr class="Table2CriteriaSetInnerRow">
                            <td class="Table2CriteriaSetSHdr">S</td>
                            <td class="Table2CriteriaSetIHdr">I</td>
                            <td class="Table2CriteriaSetRHdr">R</td>
                        </tr>
                    </table>
                </th>
                <th class = "Table2Col5Hdr">Comments</th>
            </tr>

    </HeaderTemplate>

    <ItemTemplate>
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
        <tr class = "<%# "Table2ItemRow" + Eval("IsNewDrugId").ToString() %>" >
            <td class="Table2ItemCol1"><asp:Label runat="server" ID="lblId"><%#Eval("TRGroup")%></asp:Label></td>
            <td class="Table2ItemCol2"><asp:Label runat="server" ID="Label1" class="AgentName"><%#Eval("_Agent.Name")%></asp:Label></td>
            <td class="Table2ItemCol3"><asp:Label runat="server" ID="Label2"><%#Eval("DiskContent")%></asp:Label></td>
            <td class="Table2CriteriaSetItem">
                <table class="Table2CriteriaSetTable">
                    <tr class="Table2CriteriaSetInnerRow">
                        <td class="Table2ItemS"><asp:Label runat="server" ID="Label3"><%#Eval("ZoneDiameterS")%></asp:Label></td>
                        <td class="Table2ItemI"><asp:Label runat="server" ID="Label4"><%#Eval("ZoneDiameterI")%></asp:Label></td>
                        <td class="Table2ItemR"><asp:Label runat="server" ID="Label5"><%#Eval("ZoneDiameterR")%></asp:Label></td>
                    </tr>
                </table></td>
            <td class="Table2CriteriaSetItem">
                <table class="Table2CriteriaSetTable">
                    <tr class ="Table2CriteriaSetInnerRow">
                        <td class="Table2ItemS"><asp:Label runat="server" ID="Label6"><%#Eval("MICriteriaS")%></asp:Label></td>
                        <td class="Table2ItemI"><asp:Label runat="server" ID="Label7"><%#Eval("MICriteriaI")%></asp:Label></td>
                        <td class="Table2ItemR"><asp:Label runat="server" ID="Label8"><%#Eval("MICriteriaR")%></asp:Label></td>
                    </tr>
                </table></td>
            <td class="Table2ItemCol10"><asp:Label runat="server" ID="Label9"><%#Eval("Comment")%></asp:Label></td>
        </tr>
    </ItemTemplate>
</asp:Repeater>
</table>

背后的代码:

protected void Table2List_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    //PlaceHolder1.Controls.Clear();  // Can't find this ???
    ///PlaceHolder groupHeader = (PlaceHolder)FindControl("PlaceHolder1");  // returns null???
    PlaceHolder groupHeader = (PlaceHolder)e.Item.FindControl("PlaceHolder1");  // as suggested by Servy, still returns null
    groupHeader.Controls.Clear();
    if(e.Item.ItemType == ListItemType.Item  || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Table2Row T2Data = (Table2Row)e.Item.DataItem;
        if (T2Data.IsNewGroup)
        {
            TableRow tr = new TableRow();
            tr.CssClass = "Table2GroupHeader";
            TableCell td = new TableCell();
            td.ColumnSpan = 6;
            Label lbl = new Label();
            lbl.Text = T2Data.GroupHeader;

            td.Controls.Add(lbl);
            tr.Controls.Add(td);
            groupHeader.Controls.Add(tr);
        }
    }
}

1 个答案:

答案 0 :(得分:3)

您需要使用:e.Item.FindControl("controlName"),因为控件位于转发器中。

请记住,控件不是直接在页面上,因此您将无法通过页面上的FindControl访问它。您只能通过Repeater上基于行的事件访问它,该事件提供RepeaterItemEventArgs从后面的代码访问控件。