我想在后面的代码中使用带有 ID 的 Label 控件TextLabel
,但这给了我以下异常Object reference not set to an instance of an object.
异常是在代码后面文件中的这行代码上:
Label label = e.Item.FindControl("TextLabel") as Label;
string text = label.Text;
我在这里犯了什么错误?如何在后面的代码中找到“TextLabel”控件?
asp代码:
<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;
}