Я хочу взять контроль этикетки с идентификатором TextLabel
в коде позади, но это дает мне следующее исключение Object reference not set to an instance of an object.
Исключение находится на этой строке кода в код файла:Не удается найти Метка контроля ретранслятор контроля
Label label = e.Item.FindControl("TextLabel") as Label;
string text = label.Text;
Какую ошибку я здесь сделал? Как найти элемент управления TextLabel в коде?
код ASPX:
<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>
фоновым кодом:
protected void UserPostRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
BlogProfileEntities blogProfile = new BlogProfileEntities();
Label label = e.Item.FindControl("TextLabel") as Label;
string text = label.Text;
}
Проверьте вывод HTML. Вероятно, вы увидите, что он дает ему ID, например TextLabel_1, потому что вы не можете найти его с помощью этого идентификатора. –