【问题标题】:How can i find a control in the footer template of a data repeater如何在数据转发器的页脚模板中找到控件
【发布时间】:2009-11-30 21:16:08
【问题描述】:

ASPX:代码

<asp:repeater id="repeater" runat="server">

<headerTemplate></headerTemplate>

<itemtemplate></itemtemplate>

<footerTemplate> <asp:literal id=findme runate=server> </footerTeplate>

</asp:repeater>

我正在寻找的是能够在数据转发器的页脚中找到控件的源代码。当我进行数据绑定或在页面本身中查找控件时,我熟悉基本的“FindControl”,但是如何在数据转发器的页脚模板中找到控件?

这甚至可能吗?如果是这样,我怎样才能得到一些帮助,

再次感谢大家!!!

[更新]

我需要在数据绑定之后才能做到这一点

【问题讨论】:

  • 您能否在页面类中的数据绑定期间引用该控件,然后在数据绑定后使用该引用?
  • 更新了我的帖子,并为您提供了一个示例。

标签: c# .net findcontrol datarepeater


【解决方案1】:
Protected Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
    If e.Item.ItemType = ListItemType.Footer Then
        Dim Lit As Literal = CType(e.Item.FindControl("findme"), Literal)
    End If
End Sub

【讨论】:

    【解决方案2】:

    您可以通过多种方式执行此操作,具体方式取决于您想要访问控件的时间。

    如果您在 DataBind 期间需要它,只需在项目 Databound 内执行以下操作。

    if(e.Item.ItemType == ItemType.Footer)
    {
        Literal findMe = (Literal)e.Item.FindControl("findMe");
        //Your code here
    }
    

    如果您想在另一个时间点找到它,请访问转发器的 Item 集合,然后找到“Footer”项,并从该项中找到控件。

    更新

    根据您添加的注释,您可以通过枚举项目集合来做到这一点,下面是一个带有 id 为 myRepeater 的中继器的示例。

    foreach (RepeaterItem item in myRepeater.Items)
    {
        if (item.ItemType == ListItemType.Footer)
        {
            Literal findMe = (Literal)item.FindControl("findMe");
            //Do your stuff
        }
    }
    

    【讨论】:

      【解决方案3】:

      我认为您必须在 ItemDataBound 事件处理程序中检查 ListItemType。您可以检查页眉或页脚,然后使用 FindControl 方法访问该控件。

      【讨论】:

      • 这是一个关于该主题的有趣线程。 forums.asp.net/p/1255768/2707975.aspx
      • 根据上述线程中的帖子,您只能通过转发器事件的事件处理来获取对嵌套控件的引用。我还没有确认这一点,但我确实记得在我的 ASP.NET 日子里,这是真的,因为我总是从 ItemDataBound 事件处理程序访问它们。我认为您可以在类范围内引用它们以在数据绑定发生后禁用/启用它们。
      【解决方案4】:
      Foreach (RepeaterItem item in myRepeater.Controls)
      

      这会更好,因为 Items 集合不包含页眉和页脚

      【讨论】:

        【解决方案5】:

        如果您需要获取页脚 after DataBind(这是 OP 似乎想要的),那么您可以使用以下内容:

        RepeaterItem item= (RepeaterItem)myRepeater.Controls[myRepeater.Controls.Count - 1];
        if (item.ItemType == ListItemType.Footer) {
            Literal findMe = (Literal)item.FindControl("findMe");
        }
        

        【讨论】:

          猜你喜欢
          • 2010-10-16
          • 2015-12-01
          • 1970-01-01
          • 1970-01-01
          • 2016-03-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-01-13
          相关资源
          最近更新 更多