无法在详细信息视图模板字段中找到控件
本文关键字:字段 控件 详细信息 视图 | 更新日期: 2023-09-27 17:54:54
我遇到了这个错误"对象引用不设置为一个对象的实例。"当我试图设置一个值,我从上一页的会话,并将其设置在模板字段。当我调试它时,会话值在那里(会话成功结束)。我要做的是显示我在detailsview会话的值。
aspx: <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False">
<Fields>
<asp:TemplateField HeaderText="Order Number " SortExpression="poNum">
<ItemTemplate>
<asp:Label ID="test" runat="server" Text='<%# Bind("poNum") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
背后代码:
protected void Page_Load(object sender, EventArgs e)
{
((Label)DetailsView1.FindControl("test")).Text = Session["poNum"].ToString();
}
我想知道为什么我无法从detailsview获得控件??
编辑
Label aaa = new Label();
aaa.Text = Session["poNum"].ToString();
Label orderNum = (Label)DetailsView1.FindControl("test"); // orderNum was null here
orderNum.Text = aaa.Text;
可以在DetailsView的DataBound事件中访问Label
是否在数据绑定DetailsView之前调用FindControl ?如果是,它将返回null -还没有找到任何东西。