Skip to main content
 首页 » 编程设计

c#之FindControl 找不到我动态添加的控件

2024年02月23日14zhwl

我有一个列出客户项目的中继器,其中一个是用于获取更多信息的按钮。当用户单击该按钮时,用户控件会立即出现在它的下方。我希望用户控件在再次单击按钮时关闭。我最初切换可见性服务器端,但我现在不能使用它,因为由于需要传递参数,用户控件需要在单击按钮时加载。我也不能使用 jquery 显示和隐藏,因为这将涉及首先为整个页面下载所有数据,这可能会使页面过于繁琐。

所以目前在每个转发器中我都有一个带有命令参数的链接按钮和一个触发此事件的 OnCommand 事件:

protected void uxPolicyDocsButton_Command(object sender, CommandEventArgs e) 
    { 
        //Find Policy Summary control within repeater item in order to toggle visibility 
 
        LinkButton button = sender as LinkButton; 
        if (button != null) 
        { 
            RepeaterItem ri = button.Parent as RepeaterItem; 
 
            if (ri != null) 
            { 
                PlaceHolder policySummaryPlaceHolder = (PlaceHolder)ri.FindControl("uxPolicySummaryPlaceHolder");                     
                Control policyDocuments = (Control)PolicySummaryPlaceHolder.FindControl("uxPolicyDocumentsControl"); 
 
 
                foreach (Control c in policySummaryPlaceHolder.Controls) 
                { 
                    //FindControl action goes in here.  Have stepped through though and it doesn't appear 
                } 
 
                if (policyDocuments != null) 
                { 
                    policySummaryPlaceHolder.Controls.Remove(policyDocuments); 
                } 
                else 
                { 
                    policyDocuments uxPolicyDocumentsControl = (PolicyDocuments)LoadControl("~/Controls/Home/PolicyDocuments.ascx"); 
                    uxPolicyDocumentsControl.PolicyNumber = button.CommandArgument; 
                    policySummaryPlaceHolder.Controls.Add(uxPolicyDocumentsControl); 
                } 
            } 
        } 
    } 

我的切换计划是,如果 PolicyDocuments 控件为 null,则加载该控件,如果不是,则删除该控件,但它总是返回 null。不过它确实正确加载了控件。

这是中继器的部分:

<ItemTemplate> 
                    <tr> 
                        <td> 
                            <%#Eval("StartDate","{0:d}")%> 
                        </td> 
                        <td class="center-cell-ctrl"> 
                            Postcode:<br /> 
                            <%#Eval("Postcode")%> 
                        </td> 
                        <td id='<%#Eval("PolicyNumber")%>' class="button-cell">                                 
                            <asp:LinkButton ID="uxPolicyDocsButton" CommandName="PolicyNumber" CommandArgument='<%#Eval("PolicyNumber")%>' OnCommand="uxPolicyDocsButton_Command"  runat="server" Visible="false">Policy<br />Documents</asp:LinkButton> 
                        </td> 
                    </tr> 
                    <asp:PlaceHolder ID="uxPolicySummaryPlaceHolder" runat="server"></asp:PlaceHolder> 
                    <asp:PlaceHolder ID="uxPolicyDocumentsPlaceHolder" runat="server"></asp:PlaceHolder> 
 
        </ItemTemplate> 

我四处寻找有类似问题的人,一个常见的答案是您需要在 Page_Init 事件中加载控件。这是否意味着这种方式行不通?如果是这种情况,有替代方案吗?

非常感谢

请您参考如下方法:

每次加载您的页面(无论是初始加载还是通过 PostBack)时,都需要重新创建控件树。

页面的 ASPX 部分中声明的控件是由框架添加的(某种程度上),但是您“动态”添加的任何控件也需要放回到控件树中。如果它们不存在,那么正如您所发现的那样,它们就再也找不到了。

复杂的控件,如 datagridview,通过在 ViewState 中存储(大量)数据来重建它们的控件树。

与其让您的页面在 PlaceHolder 中添加/删除项目,不如创建一个您可以隐藏的用户控件,并在第一次显示时加载它的数据,这可能更简单。这也有助于您的控件和页面遵守 Single Repsonsibility Principal