C# 在asp.net转发器中找不到控件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1203316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:48:50  来源:igfitidea点击:

Can't find control within asp.net repeater?

c#asp.netrepeater

提问by Xaisoft

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:

我在下面有以下中继器,我试图在后面的代码中找到 lblA 但它失败了。标记下方是我所做的尝试:

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

First I tried,

首先我试过,

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

but lblA was null

但 lblA 为空

Then I tried,

然后我试了一下,

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

but Items was 0 even though m repeater contains 1 itemtemplate

但即使 m 中继器包含 1 个项目模板,项目为 0

采纳答案by Spencer Ruport

You need to set the attribute OnItemDataBound="myFunction"

您需要设置属性 OnItemDataBound="myFunction"

And then in your code do the following

然后在您的代码中执行以下操作

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

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

顺便说一句,您可以将这种完全相同的方法用于嵌套中继器。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>

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.

我经常看到人们在内部中继器上手动绑定项目,但他们没有意识到为自己制作东西有多么困难。

回答by Dan Diplo

回答by Kyle B.

Code for VB.net

VB.net 代码

    Protected Sub rptDetails_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptDetails.ItemDataBound    
      If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
        Dim lblA As Label = CType(e.Item.FindControl("lblA"), Label)
        lblA.Text = "Found it!"
      End If
    End Sub

回答by Peter

You should bind first.
for example)

你应该先绑定。
例如)

rptDetails.DataSource = dataSet.Tables["Order"];

rptDetails.DataBind();

回答by dvdmn

I just had the same problem.

我只是遇到了同样的问题。

We are missing the item typewhile looping in the items. The very first item in the repeater is the header, and header does not have the asp elements we are looking for.

我们在项目中循环时缺少项目类型。转发器中的第一项是header,而 header 没有我们正在寻找的 asp 元素。

Try this:

尝试这个:

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {Label lblA = (Label)rptDetails.Items[0].FindControl("lblA");}