找不到标签控制在Repeater控件

找不到标签控制在Repeater控件

本文介绍了找不到标签控制在Repeater控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要随身携带身份证为textLabel 在code后面的标签控制,但是这给我下面的异常未将对象引用设置到对象的实例唯一的例外是在隐藏文件code此行code的:

 标签标签= e.Item.FindControl(为textLabel)作为标签;  字符串文本= label.Text;

什么错误,我在这里做?如何找到背后code为textLabel控制?

ASPX code:

 < ASP:直放站ID =UserPostRepeater=服务器OnItemDataBound =UserPostRepeater_ItemDataBound>
    <&HeaderTemplate中GT;
    < / HeaderTemplate中>
    <&ItemTemplate中GT;        < ASP:标签ID =为textLabel=服务器文本=标签>< / ASP:标签>
    < / ItemTemplate中>
    < FooterTemplate>
    < / FooterTemplate>
< / ASP:直放站>

code-背后:

 保护无效UserPostRepeater_ItemDataBound(对象发件人,RepeaterItemEventArgs E)
{
    BlogProfileEntities blogProfile =新BlogProfileEntities();
    标签LABEL = e.Item.FindControl(为textLabel)作为标签;
    字符串文本= label.Text;
}


解决方案

在使用的ItemDataBound 您需要检查中继器项目的类型 - e.Item.ItemType

它需要为 ListItemType.Item ListItemType.AlternatingItem - 这是它的标签是存在的模板

 保护无效UserPostRepeater_ItemDataBound(对象发件人,RepeaterItemEventArgs E)
{
    BlogProfileEntities blogProfile =新BlogProfileEntities();    如果(e.Item.ItemType == || ListItemType.Item
        e.Item.ItemType == ListItemType.AlternatingItem)
    {
      标签LABEL = e.Item.FindControl(为textLabel)作为标签;
      字符串文本= label.Text;
    }
}

I want to take the Label control with ID TextLabel in the code behind, but this give me the following exception Object reference not set to an instance of an object. The exception is on this line of code in code behind file:

  Label label = e.Item.FindControl("TextLabel") as Label;

  string text = label.Text;

What mistake I made here ? How to find "TextLabel" control in code behind ?

aspx code:

<asp:Repeater ID="UserPostRepeater" runat="server" OnItemDataBound="UserPostRepeater_ItemDataBound">
    <HeaderTemplate>
    </HeaderTemplate>
    <ItemTemplate>

        <asp:Label ID="TextLabel" runat="server" Text="Label"></asp:Label>
    </ItemTemplate>
    <FooterTemplate>
    </FooterTemplate>
</asp:Repeater>

code-behind:

protected void UserPostRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    BlogProfileEntities blogProfile = new BlogProfileEntities();
    Label label = e.Item.FindControl("TextLabel") as Label;
    string text = label.Text;
}
解决方案

When using the ItemDataBound you need to check the type of repeater item - e.Item.ItemType.

It needs to be either ListItemType.Item or ListItemType.AlternatingItem - these are the templates where the label exists.

protected void UserPostRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    BlogProfileEntities blogProfile = new BlogProfileEntities();

    if (e.Item.ItemType == ListItemType.Item ||
        e.Item.ItemType == ListItemType.AlternatingItem)
    {
      Label label = e.Item.FindControl("TextLabel") as Label;
      string text = label.Text;
    }
}

这篇关于找不到标签控制在Repeater控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 08:48