在FormView中找不到控件?

wal*_*ter 3 c# asp.net formview findcontrol

我需要找到这个<a>标签驻留在一个FormView控件中,我需要根据条件删除这个标签,但我找不到它使用FormView.FindControl方法

<asp:UpdatePanel ID="upDiscipline" runat="server">
   <ContentTemplate>
        <asp:FormView ID="fvMediaIntro" runat="server">
           <ItemTemplate>           
                  <div class="clipControls">
                     <a runat="server" id="iNeedToFindThis" href="#">here</a>
                  </div>
           </ItemTemplate>
   </ContentTemplate>
</asp:UpdatePanel>
Run Code Online (Sandbox Code Playgroud)

我试过fvMediaIntro.FindControl()fvMediaIntro.Row.FindControl(),既不工作.有什么想法吗?

Vin*_*ayC 7

FindControl只有在创建了这些控件之后才会工作,即数据被绑定到FormView.所以,你需要在适当的使用情况FormViewItemCreatedDataBound.例如,

protected void fvMediaIntro_ItemCreated(Object sender, EventArgs e)
{
   var control = fvMediaIntro.Row.FindControl("iNeedToFindThis") as HtmlAnchor;
}
Run Code Online (Sandbox Code Playgroud)

假设您绑定page_load或使用标记,您还可以prerender安全地使用父页面/控件事件FindControl.