找不到asp.net中继器内的控制? [英] Can't find control within asp.net repeater?

查看:103
本文介绍了找不到asp.net中继器内的控制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面我有以下中继器,我试图找到LBLA背后code和失败。下面的标识是我所作出的尝试:

I have the following repeater below and I am trying to find lblA in code behind and it fails. Below the markup are the attempts I have made:

<asp:Repeater ID="rptDetails" runat="server">
    <HeaderTemplate>
        <table>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td><strong>A:</strong></td>
            <td><asp:Label ID="lblA" runat="server"></asp:Label>
            </td>
        </tr>
    </ItemTemplate>
</asp:Repeater>
</table>

首先我试过了,

Label lblA = (Label)rptDetails.FindControl("lblA");

但LBLA为空

然后我尝试,

Label lblA = (Label)rptDetails.Items[0].FindControl("lblA");

但项目为0,即使米直放站包含1的ItemTemplate

but Items was 0 even though m repeater contains 1 itemtemplate

推荐答案

您需要设置属性 OnItemDataBound =myFunction的

,然后在code执行以下操作

And then in your code do the following

void myFunction(object sender, RepeaterItemEventArgs e)
{
   Label lblA = (Label)e.Item.FindControl("lblA");
}

顺便说一句,你可以使用嵌套中继器此相同的做法。 IE浏览器:

Incidentally you can use this exact same approach for nested repeaters. IE:

<asp:Repeater ID="outerRepeater" runat="server" OnItemDataBound="outerFunction">
<ItemTemplate>
   <asp:Repeater ID="innerRepeater" runat="server" OnItemDataBound="innerFunction">
   <ItemTemplate><asp:Label ID="myLabel" runat="server" /></ItemTemplate>
   </asp:Repeater>
</ItemTemplate>
</asp:Repeater>

,然后在code:

And then in your code:

void outerFunction(object sender, RepeaterItemEventArgs e)
{
   Repeater innerRepeater = (Repeater)e.Item.FindControl("innerRepeater");
   innerRepeater.DataSource = ... // Some data source
   innerRepeater.DataBind();
}
void innerFunction(object sender, RepeaterItemEventArgs e)
{
   Label myLabel = (Label)e.Item.FindControl("myLabel");
}

很多时候我看到人们在其内转发手动绑定表项,他们不知道有多难他们正在为自己做的事情。

All too often I see people manually binding items on an inner repeater and they don't realize how difficult they're making things for themselves.

这篇关于找不到asp.net中继器内的控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆