【问题标题】:can't find control in FormView?在 FormView 中找不到控件?
【发布时间】:2011-08-31 15:05:41
【问题描述】:

我需要在FormView 控件中找到这个<a> 标签,我需要根据条件删除这个标签,但我无法使用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>

我试过fvMediaIntro.FindControl()fvMediaIntro.Row.FindControl(),都没有成功。 请问有什么办法吗??

【问题讨论】:

    标签: c# asp.net formview findcontrol


    【解决方案1】:

    FindControl 仅在创建这些控件后才能工作,即当数据绑定到 FormView 时。因此,您需要在FormView 上使用适当的事件,例如ItemCreatedDataBound。例如,

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

    假设你在page_load绑定或者使用标记,你也可以使用父页面/控件的prerender事件安全地做FindControl

    【讨论】:

    • 我可以遍历 Formview 中的所有行吗?
    • @PankajGarg,我不确定你所说的所有行是什么意思! FormView 一次只显示一个数据行 - 可以通过Row 属性访问。其他行对象可能基于设置存在,例如HeaderRowFooterRowTopPagerRow
    猜你喜欢
    • 2014-11-14
    • 2013-09-22
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 2021-10-02
    • 2010-12-08
    相关资源
    最近更新 更多