1

我正在构建一个包含转发器的 ASP.NET 用户控件。在 ItemTemplate 中,我放置了一个 PlaceHolder。只要数据源中的布尔字段为真,我就会尝试在占位符中显示组标题。

中继器工作正常,加载并显示除组头之外的所有数据。但是,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);
        }
    }
}
4

1 回答 1

3

您需要使用:e.Item.FindControl("controlName")因为控件在中继器中。

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

于 2012-10-23T20:22:47.877 回答