如何在数据中继器的页脚模板中找到控件

发布于 2024-08-12 17:56:06 字数 571 浏览 9 评论 0原文

ASPX:代码

;

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

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

再次感谢大家!

[更新]

我需要能够在数据绑定后执行此操作

ASPX : Code

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

<headerTemplate></headerTemplate>

<itemtemplate></itemtemplate>

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

</asp:repeater>

What i am looking for is source code to be able to find the control within the footer of a data repeater. Im familiar with the basic "FindControl" when i do a databind or look for control within the page itself, but how can i find the control within a footer template of a data repeater?

Is this even possible? and if so how can i please get some assistance,

thanks again to all!!!

[update]

i need to be able to do this after the databind

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一城柳絮吹成雪 2024-08-19 17:56:06
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
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
ゃ懵逼小萝莉 2024-08-19 17:56:06

有多种方法可以做到这一点,具体方法取决于您想要何时访问该控件。

如果您希望在 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
    }
}

There are a number of ways that you can do it, the exact way depends on when you want to get access to the control.

If you want it during DataBind, simply do the following inside the item Databound.

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

If you want to find it at another point in time, access the repeater's Item collection, then find the "Footer" item, and from that item, you can find the control.

Update

Based on your added note, you can do this by enumerating the item collection, below is an example with a repeater that has an id of myRepeater.

foreach (RepeaterItem item in myRepeater.Items)
{
    if (item.ItemType == ListItemType.Footer)
    {
        Literal findMe = (Literal)item.FindControl("findMe");
        //Do your stuff
    }
}
最美的太阳 2024-08-19 17:56:06

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

I think you have to check the ListItemType in an ItemDataBound event handler. You can check for Header or Footer and then use the FindControl method to access the control.

为你拒绝所有暧昧 2024-08-19 17:56:06
Foreach (RepeaterItem item in myRepeater.Controls)

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

Foreach (RepeaterItem item in myRepeater.Controls)

This will work better as Items collection doesn't contain header and footer

黑色毁心梦 2024-08-19 17:56:06

如果您需要在 DataBind 之后获取页脚(这是 OP 似乎想要的),那么您可以使用以下命令:

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

If you need to get the Footer after DataBind (which is what the OP appears to want) then you can use the following:

RepeaterItem item= (RepeaterItem)myRepeater.Controls[myRepeater.Controls.Count - 1];
if (item.ItemType == ListItemType.Footer) {
    Literal findMe = (Literal)item.FindControl("findMe");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文