can't find control in FormView?

拈花ヽ惹草 提交于 2019-12-18 06:57:58

问题


I need find this <a> tag resided in a FormView control, I need to remove this a tag depending on the condition but I can't find it using FormView.FindControl method

<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>

I tried fvMediaIntro.FindControl() and fvMediaIntro.Row.FindControl(), neither worked. Any idea please??


回答1:


FindControl will work only after those controls are created i.e when data is bound to the FormView. So you need to use appropriate event on FormView such ItemCreated or DataBound. For example,

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

Assuming, you are binding in page_load or using mark-up, you can also use prerender event of parent page/control safely to do FindControl.



来源:https://stackoverflow.com/questions/6105634/cant-find-control-in-formview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!